Difference between IEnumerable and IQueryable in C# with example

Introduction: In this article I am going to explain what is the difference between IEnumerable and IQueryable Or we can say we will discuss IEnumerable vs IQueryable.

Both IEnumerable and IQueryable are interfaces and used for data manipulation (like filtering etc.in LINQ from the database and collections.

Let’s understand some points about these IEnumerable and IQueryable one by one.


IEnumerable:

1) To use IEnumerable in our application, we need to add System.Collections Namespace.

2) IEnumerable doesn’t move backward or between items, it is forward only collection.

3) IEnumerable is best suitable for querying data from in-memory collections like List, Array etc.

4) While querying data from database, IEnumerable first executes “select query” on server side then it loads all data into memory client side and then it apply filters on that data. Hence it does more work so a little bit slow as compared to IQueryable.

5) IEnumerable is suitable for LINQ to Object and LINQ to XML queries.

6) IEnumerable supports deferred execution.

7) IEnumerable does not support custom query.

8) IEnumerable does not support lazy loading because it fetches all the record. So it is not suitable for paging like scenarios.

9) Extension methods supported by IEnumerable takes functional objects. Query is constructed using delegates.


Example:

EmployeeContext db = new EmployeeContext();
IEnumerable<employee> list = db.Employees.Where(x => x.Department.Equals(“SALES”));
empList = empList. Take<employee>(5);

On executing above code we will get sql query like as shown below.

SELECT [t0].[EmployeeId], [t0].[EmployeeName], [t0].[Age], [t0].[Gender]
FROM [dbo].[Employees] AS [t0]
WHERE [t0].[Department] = @p0


Note: "top 5" is missing since IEnumerable filters records on client side i.e. it will apply filtering conditions once it loads all the data in client-side memory.


IQueryable:

IQueryable is an interface that implements IEnumerable. So it is clear that whatever IEnumerable can do, IQueryable can also do plus some more.

1) To use IQueryable we need to add System.Linq Namespace.

2) Similar to IEnumerable, IQueryable doesn’t move backward or between items, it is also forward only collection.

3) IQueryable is best to query data from out-memory like remote database or web service etc.

4) While querying data from database, IQueryable executes “select query” on server side with all the filters. This way we can apply filters to our query before the query is executed. So less operation required and it also reduces network traffic. So it is fast as compared to IEnumerable.

5) IQueryable is suitable for LINQ to SQL queries.

6) IQueryable also supports deferred execution.

7) IQueryable supports custom query using CreateQuery and Execute methods.

8) IQueryable supports lazy loading. I.e. we only get what we need, not the entire list of records. Hence it is suitable for paging like scenarios.

9) Extension methods supported by IQueryable takes expression objects i.e. It constructs the query using an Expression Tree.

Example:

EmployeeContext db = new EmployeeContext();
IQueryable<employee> empList = db.Employees.Where(x => x.Department.Equals(“SALES”));
empList = empList. Take<employee>(5);

On executing above code we will get sql query like as shown below.

SELECT TOP (5) [t0].[EmployeeId], [t0].[EmployeeName], [t0].[Age], [t0].[Gender]
FROM [dbo].[Employees] AS [t0]
WHERE [t0].[Department] = @p0


Note: "top 5" exists in above query since IQueryable executes query in SQL server with all filters to get only matching records instead of loading all the data into memory.

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates. 
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..