Which LINQ statements force Entity Framework to return from the DB?

前端 未结 4 1481
旧巷少年郎
旧巷少年郎 2020-12-09 10:02

I know of several LINQ statements that will cause EF to evaluate and return results form the DB to memory. .ToList() is one. Does anyone have a comprehensive

4条回答
  •  情歌与酒
    2020-12-09 10:14

    Anything that returns a concrete object or data structure (Count, Sum Single, First, ToList, ToArray, etc.) is evaluated immediately, so SingleOrDefault certainly does.

    Anything that returns an IQueryable (Select, GroupBy, Take) will be deferred (so that operations can be chained), so Queryable.Union will be deferred.

    Anything that returns an IEnumerable will also be deferred, but subsequent queries will be done in Linq-to-objects, so subsequent operations won't be translated to SQL. (Empty is an exception since there's not really anything to defer - it just returns an empty collection)

提交回复
热议问题