Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IQueryable vs. IEnumerable in the repository pattern , lazy loading

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?

like image 648
Milan Mendpara Avatar asked Jan 20 '12 21:01

Milan Mendpara


2 Answers

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

like image 196
Robert Harvey Avatar answered Oct 22 '22 13:10

Robert Harvey


IEnumerable vs IQueryable

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

like image 37
Taja_100 Avatar answered Oct 22 '22 11:10

Taja_100