Se establece una conexión cuando realmente empiezas a enumerar y cuando presionas SubmitChanges (si hay cambios que realizar). No estoy seguro de si en el código anterior solo se abre y utiliza UNA conexión, pero sé que en esos dos lugares que mencioné invocará una conexión.
Necesita comenzar a buscar en LinqPad y how to use it en dimecasts. También puedes ver su serie en la Delayed Execution features of Linq 2 Sql
Nota, algo como esto (getTenSomethingElse (s, s, s)) no consulta la base de datos, al menos no hasta que empiece a enumerar el valor de retorno
partial class MyDataContext
{
// builds the tree to pull data from the db that matches a criteriea and pass it a ctor of another class
public System.Linq.IQueryable<SomethingElse> getSomethingElse(string searchTerm, string searchValue, string orderBy)
{
var items =
from s in
this.Somethings
select new SomethingElse(s);
return items.Where(searchTerm, searchValue).OrderBy(orderBy);
}
// calls the above method but adds take 10 to that tree
public System.Linq.IQueryable<SomethingElse> getTenSomethingElse(string searchTerm, string searchValue, string orderBy)
{
var items =
from s in
this.getSomethingElse(searchTerm, searchValue, orderBy)
select s;
return items.Take(10);
}
}
IDK sobre usted, pero considero que es bastante increíble teniendo en cuenta todo el trabajo que se está haciendo.
Ah por cierto, más información sobre eso "Donde (s, s)" extensión se puede encontrar en ScottGu's awesome blog
No es un L2S pro, pero espero * solo abrir y cerrar en SubmitChanges(). – mxmissile