Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Differences between IQueryable and IEnumerable in C# with Example

Sep 26, 2016
Introduction:

Here I will explain difference between IQueryable and IEnumerable list in c# with example or IEnumerable vs IQueryable list in c# with example or what is difference between IQueryable and IEnumerable list in c# with example. In c# we use IQueryable and IEnumerable lists to perform data manipulation.

Description:


Generally, we use IEnumerable and IQueryable to hold collection of data and perform data manipulation operations like filtering, etc. based on our requirements. We will see difference between IEnumerable and IQueryable with examples.

IEnumerable

1. If we want to use IEnumerable in our application, we can get it by adding System.Collections namespace.


2. IEnumerable best suitable for in-memory operations because first it will execute “select query” on server and it will load all the data into client memory then only it will apply all the filter conditions.

Suppose if we have table called EmployeeDetails in our database from that we need to get only top 3 records where users gender equals to “Male” for this if we use IEnumerable first it will execute “select query” on database server, it loads all the records into memory and then it filters the data based on our requirement.



3. For remote operations IEnumerable is not suggestable and its better use IEnumerable to perform query operations on in-memory collections like List, Array, etc.

In case if our tables contain more than 1 or 2 lac records then IEnumerable will fetch all the records into our application memory then it will perform filter conditions due to this our application becomes very slow.

4. IEnumerable is beneficial when you want to load all the data from query into memory and apply further filtering. Its best suitable for LINQ to Objects and LINQ to XML operations.

IEnumerable Example

Following is the simple example for IEnumerable collection.


DataClasses1DataContext dbcon = new DataClasses1DataContext();
IEnumerable<EmployeeDetail> emplist = dbcon.EmployeeDetails.Where(e => e.Gender.Equals("Male"));
emplist = emplist.Take<EmployeeDetail>(3);

When we execute above code we will get sql query like as shown below


SELECT [t0].[EmpId], [t0].[EmpName], [t0].[Location], [t0].[Gender]
FROM [dbo].[EmployeeDetails] AS [t0]
WHERE [t0].[Gender] = @p0


Here if you observe above query “Top 3” filtering condition is missing because IEnumerable will apply filtering conditions once it loads all the data in client-side memory.

IQueryable

1. If we want to use IQueryable in our application, we can get it by adding System.Linq namespace.



2. IQueryable is best suitable for out-memory (remote database) operations because it will execute select query with all filter conditions on server.

Suppose if we have table called EmployeeDetails in our database from that we need to get only top 3 records where users gender equals to “Male” for this if we use IQueryable it will execute “select query along with filter conditions” on database server and it loads only required records based on our conditions.



3. IQueryable is best suitable for LINQ to SQL operations.

IQueryable Example

Following is the simple example for IQueryable collection.


DataClasses1DataContext dbcon = new DataClasses1DataContext();
IQueryable<EmployeeDetail> emplist = dbcon.EmployeeDetails.Where(e => e.Gender.Equals("Male"));
emplist = emplist.Take<EmployeeDetail>(3);

When we execute above code we will get sql query like as shown below


SELECT TOP (3) [t0].[EmpId], [t0].[EmpName], [t0].[Location], [t0].[Gender]
FROM [dbo].[EmployeeDetails] AS [t0]
WHERE [t0].[Gender] = @p0

Here if you observe above query “Top 3” filtering condition also included it means IQueryable will apply all the filtering conditions on SQL Server itself to get only matching records instead of loading all the data into memory.

I hope it helps you to understand difference between IEnumerable and IQueryable.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

17 comments :

Anonymous said...

th9x

Unknown said...

thanks very simple and easy to understand....

Ketan said...

nice to explain and easy to understand

Anonymous said...

well done :-)

Unknown said...

Genius suresh,you are doing well. .keep it up dude

Unknown said...

Thank You!
Ur Way of Explaining things is very easy in terms of language as well as point to point.

Suma said...

thank you Suresh

Anonymous said...

explanation is down to earth.

Unknown said...

Thank suresh..

Unknown said...

Its really great explanation.. Thank you so much.

Anonymous said...

Informative and easily understandable

Unknown said...

nice explanation. keep it up dude

Anonymous said...

Given both 'WHERE' and 'TOP 10' perform filtration, why can't IEnumerable execute 'WHERE' clause filtration?

SR Blog said...

Thanks nice explanation and example.

Anonymous said...

Nice explanation... Thank you very much....

Debasmit said...

Excellent

Rahul Patil said...

@SURESH DASARI what is remote database can u give any refernace link??i dont know what is remote database???

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.