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

前端 未结 4 1487
旧巷少年郎
旧巷少年郎 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:20

    From MSDN,

    Queries that perform aggregation functions over a range of source elements must first iterate over those elements.

    Examples of such queries are Count, Max, Average, and First. These execute without an explicit foreach statement because the query itself must use foreach in order to return a result.

    Note also that these types of queries return a single value, not an IEnumerable collection.

    To force immediate execution of any query and cache its results, you can call the ToList or ToArray methods.

提交回复
热议问题