Sunday, 25 September 2016

C#: What is the difference between IQueryable vs IEnumerable?

The main difference between the two is the location of the query execution.

The following example demonstrates the difference:

IQueryable<Model> models = ...
models.Where(x => x.ID ...)

IQueryable is used from LINQ-to-Anything (such as SQL). In this case, the query filter will be executed on the SQL server. This is great for reducing the amount of data returned from the data store.

However, if the code looks as follows:
IEnumerable<Model> models = ...
models.Where(x=> x.ID ...)

The results might be the same but using IEnumerable will cause the query results to be loaded in the memory and the predicate will be executed on the entire result set.

No comments:

Post a comment