.NET Entity Framework - IEnumerable VS. IQueryable

前端 未结 4 970
夕颜
夕颜 2020-12-10 10:50

Please see this line of code. This is an invocation of a stored procedure, which returns an ObjectResult. In order to extract the long values I add

4条回答
  •  孤街浪徒
    2020-12-10 11:19

    Working on an IEnumerable means that all further operations will happen in C# code, i.e. linq-to-objects. It does not mean that the query has already executed.

    Once you degrade to linq-to-objects all data left at this point needs to be fetched from the database and sent to .net. This can degrade performance drastically(For example database indexes won't be used by linq-to-objects), but on the other hand linq-to-objects is more flexible, since it can execute arbitrary C# code instead of being limited by what your linq provider can translate to SQL.

    A IEnumerable can be both a deferred query or already materialized data. The standard linq operators typically are deferred, and ToArray()/ToList() are always materialized.

提交回复
热议问题