建立连接,当你打的SubmitChanges(如果有要进行更改)。我不知道,如果在上面的代码中只有一个连接被打开和使用,但我知道在我提到的两个地方,你会调用连接。
您需要开始查看LinqPad和how to use it而不是dimecasts。还检查了他们的一系列关于Delayed Execution features of Linq 2 Sql
注意,这样的事情(getTenSomethingElse(S,S,S))不查询数据库,至少直到你开始列举了返回值
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你,但我认为这是相当真棒考虑正在完成所有的工作,多数民众赞成。
哦顺便说一句,对“在哪里(S,S)”扩展的更多信息可以在ScottGu's awesome blog
不是L2S专业版,但我希望它只能在SubmitChanges()中打开和关闭。 – mxmissile 2009-09-16 16:31:46