blog

Home / DeveloperSection / Blogs / Difference between IQueryable<T> vs IEnumerable<T>

Difference between IQueryable<T> vs IEnumerable<T>

Anupam Mishra3848 25-Mar-2016

A lots of time Confusion between IQueryable and IEnumerable interface because, they are look like same and when we start writing a code often, we will choose a wrong approach between them. From behind this reason was I am unable to know perfectly about these two. Here, we will try to explain a differences. 

 IQueryable interface:

IQueryable exists in System.Linq Namespace. It only forward only over a collection. This is a better query data from out-memory (like remote database, service) collections. When we access a data from a database IQueryable execute select query on server side with all filters. It support Lazy Loading that was better for paging.

IQueryable<T> is the interface that allows LINQ-to-SQL to work. If we want to refine another query it will executes, if necessary.

 In code:

 

 IQueryable<Student> stud = ………….
 var studentGrade = db.Students.stud .Where(x =>x.grade<5);

 

That code will execute SQL to only select Students which grade is <5. And


we have also filter all other students which grade above from 5. 

IEnumerable interface:


Enumerable exists in System.Collections Namespace. It only forward only over a collection. IEnumerable is best to query data from in-memory collections. While we access data from database, IEnumerable interface execute select query on server side and load data in-memory on client side and then filtering data. IEnumerable is suitable for LINQ to Object queries. IEnumerable supports deferred execution. IEnumerable does not support custom query and also be a lazy loading. 

 IEnumerable<T> is the interface that allows LINQ-to-Object to work. It means that all objects matching the original query will have to be loaded into memory from the database.


  IEnumerable<Student> stud = ……….
 var studentGrade = db.Students.stud .Where(x =>x.grade<5);

 

This is quite an important difference, and working on IQueryable<T> can in many cases save you from returning too many rows from the database. Another prime example is doing paging: If you use Take andSkip on IQueryable, you will only get the number of rows requested; doing that on an IEnumerable<T> will cause all of your rows to be loaded in memory. 

Differences:

 There are many differences as below:

IEnumerable: 

 1.   IEnumerable is exists under the System.Collections namespace.   

2.   IEnumerable is suitable for querying data from in-memory collections.

3.   While querying data from the database, IEnumerable executes "select query" on the server-side,  loads data in-memory on the client-side and then filters the data.

4.   IEnumerable is also be better work for LINQ to Object and LINQ to XML queries.        

  IQueryable:
1.      IQueryable is exists under the System.Linq Namespace.
2.      IQueryable is suitable for querying data from out-memory (like remote
database, service) collections.
3.      While querying data from a database, IQueryable executes a "select
query" on server-side with all  filters.
 4. IQueryable is beneficial for LINQ to SQL queries.

Updated 14-Mar-2018

Leave Comment

Comments

Liked By