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???
我正在使用实体框架 5
我开始学习Linq。我真的很困惑,哪些立即执行函数每次都会调用 SQL。正如您在上面的示例中看到的,ToList() 和 Count() 都是立即执行函数,但只有 Count() 会在子序列调用时连接到 sql。ToList() 连接 sql 1 次,但 Count() 每次都会连接 Sql。
如何确定哪些 linq 函数不会多次调用 sql?