var costCenters = from c in dbContext.CostCenters //no sql call here
orderby c.DisplayRank descending
select c;
List<CostCenter> lstCostCenter = costCenters.ToList();//Immediate execution to sql the first time
lstCostCenter = costCenters.ToList();//no Sql call ??
int test = costCenters.Count();//Call Sql everytime
test = costCenters.Count();//Call Sql again???
我正在使用Entity Framework 5。
我开始学习Linq。 我真的很困惑哪些立即执行函数每次都会调用SQL。 如您在上面的示例中所看到的,ToList()和Count()均为立即执行函数,但仅Count()将在后续调用时连接到sql。 ToList()只连接到sql一次,但Count()每次都会连接到Sql。
如何确定哪些linq函数不会多次调用sql?
ToList
回调 SQL,那么为什么它需要为第二个Count
回调 SQL 呢? - Rawling