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?
我开始学习LINQ的。我真的很困惑,哪些即时执行函数每次都会调用SQL。正如你在上面的示例中看到的,ToList()和Count()都是立即执行函数,但是只有Count()会在子序列调用上连接到sql。 ToList()连接到sql 1次,但Count()将每次连接到Sql。
如何识别哪些linq函数不会多次调用sql?
进一步阅读后,我意识到缓存是由“强制执行” – gavin