Friday, May 3, 2013

IEnumerable & IQueryable pros &Cons

IEnumerable 
Pros
1.       The most generic item of all.
2.       Still might use deferred execution.
3.       IEnumerable is best suitable for working with queries later on. It helps best when we try to add more queries with our previous one and we do not want to run our previous query in realtime but when we requested it or iterated through those or finally passing as a .ToList() object .
4.       IEnumerable does not run query until it is requested by iteration.
Cons
1.       IEnumerable doesn’t move between items, it is forward only collection as LinkedList. You can't get at "item 4" without passing items 0-3.
2.       Read-only list, you can't add to it or remove from it.
3.       Actually the model binder is not able to bind an enumerable because it is a too generic Interface, and it is not able to choose a concrete class to create that implements the IEnumerable.
 
 
IQueryable 
 
 
Pros
1.       Query isn't executed until to really iterate over the items, maybe by doing a .ToList()
2.       IQueryable best suits for remote data source, like a database or web service. IQueryable is a very powerful feature that enables a variety of interesting deferred execution scenarios (like paging and composition based queries).
Cons

1.       avoid passing IQueryable to Views
2.       It is not a good place to handle errors...so better getting rid of delayed execution before passing to Views.

No comments:

Post a Comment