IQueryable vs. IEnumerable in the repository pattern , lazy loading

喜夏-厌秋 提交于 2019-12-29 05:07:18

问题


I have read some articles that state that IEnumerable used to mimic stored procedures or restrict your database. Lost lazy loading ability on the external provider.

Where as IQueryable to give developers more flexibility. Lazy loading is there.

In terms of performance, both consume a significant amount of performance .. so which one is more preferable?


回答1:


From the perspective of a Repository Pattern, you can think of it this way:

  1. Use an eager loading IEnumerable when you want to pass an entire list to the client in one go. They can still add linq clauses, but the client does not benefit from deferred execution.

  2. Use a lazy loading IQueryable when you want to extend deferred querying capabilities to the client, by allowing the client to add their own linq clauses. This defers execution of the entire query until output is required.

See Also
Deferred execution and eager evaluation




回答2:


IQueryable is best for server side data, whereas IEnumerable is best for arrays/lists that uses on client side.




回答3:


The main difference between “IEnumerable” and “IQueryable” is about where the filter logic is executed. One executes on the client side(in memory) and the other executes on the database.

So when querying data from in-memory collections like List, Array etc you should use IEnumerable.

On the other hand when querying data from out-memory (like remote database, service) collections you should use IQueryable.



来源:https://stackoverflow.com/questions/8947923/iqueryable-vs-ienumerable-in-the-repository-pattern-lazy-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!