Click here to Skip to main content
15,890,438 members
Articles / All Topics

IEnumerable Vs IQueryable

Rate me:
Please Sign up or sign in to vote.
4.82/5 (103 votes)
24 Feb 2014CPOL1 min read 262K   49   13
IEnumerable Vs IQueryable

Many developers get confused between IEnumerable and IQueryable. When it comes to writing code, both look very similar. However, there are many differences between them which need to be taken care of while writing code. Both have some intended usability scenarios for which they are made.

Listed below are the differences between them based on their properties:

  IEnumerable IQueryable
Namespace System.Collections Namespace System.Linq Namespace
Derives from No base interface Derives from IEnumerable
Deferred Execution Supported Supported
Lazy Loading Not Supported Supported
How does it work While querying data from database, IEnumerable executes select query on server side, load data in-memory on client side and then filter data. Hence does more work and becomes slow. While querying data from database, IQueryable executes select query on server side with all filters. Hence does less work and becomes fast.
Suitable for LINQ to Object and LINQ to XML queries LINQ to SQL queries
Custom Query Doesn’t support Supports using CreateQuery and Execute methods
Extension method
parameter
Extension methods supported in IEnumerable takes functional objects. Extension methods supported in IEnumerable takes expression objects, i.e., expression tree.
When to use When querying data from in-memory collections like List, Array, etc. When querying data from out-memory (like remote database, service) collections.
Best Uses In-memory traversal Paging

This article was originally posted at http://synvistech.com/blogs/ienumerable-vs-iqueryable

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionThank you Pin
tripe3way13-Dec-19 1:35
tripe3way13-Dec-19 1:35 
PraiseGood effects Pin
hebsiboy22-Oct-16 19:12
hebsiboy22-Oct-16 19:12 
GeneralMy vote of 5 Pin
girishmeena28-Aug-16 4:54
girishmeena28-Aug-16 4:54 
QuestionIEnumerable Linq capabilities Pin
girishmeena28-Aug-16 4:52
girishmeena28-Aug-16 4:52 
QuestionThanks Pin
Pawan Tiwari9315-May-16 19:49
professionalPawan Tiwari9315-May-16 19:49 
QuestionGood Short Descriptive Pin
Er. Puneet Goel6-Apr-16 0:46
professionalEr. Puneet Goel6-Apr-16 0:46 
GeneralGood Work Pin
Alireza_13623-Apr-16 8:21
Alireza_13623-Apr-16 8:21 
GeneralMy vote of 5 Pin
Chinh Vo Wili27-Jan-16 4:01
professionalChinh Vo Wili27-Jan-16 4:01 
GeneralRe: My vote of 5 Pin
Adarsh Chaurasia - Passionate Trainer22-Mar-16 5:58
professionalAdarsh Chaurasia - Passionate Trainer22-Mar-16 5:58 
Suggestiongood article Pin
toumir24-Nov-15 22:38
toumir24-Nov-15 22:38 
QuestionDifference between IEnumerable VS IQueryable:- Pin
Member 1057063910-Aug-15 18:23
Member 1057063910-Aug-15 18:23 
IEnumerable VS IQueryable

LINQ to query data from database and collections, we use IEnumerable and IQueryable for data manipulation. IEnumerable is inherited by IQueryable, Hence IQueryable has all the features of IEnumerable and except this, it has its own features. Both have its own importance to query data and data manipulation. Let’s see both the fetures and take the advantage of both the fetures to boost your LINQ Query performance.
IEnumerable
1. IEnumerable exists in System.Collections Namespace.
2. IEnumerable can move forward only over a collection, it can’t move backward and between the items.
3. IEnumerable is best to query data from in-memory collections like List, Array etc.
4. While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data.
5. IEnumerable is suitable for LINQ to Object and LINQ to XML queries.
6. IEnumerable supports deferred execution.
7. IEnumerable doesn’t supports custom query.
8. IEnumerable doesn’t support lazy loading. Hence not suitable for paging like scenarios.
9. Extension methods supports by IEnumerable takes functional objects.
IEnumerable Example
1. MyDataContext dc = new MyDataContext ();
2. IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
3. list = list.Take<Employee>(10);
Generated SQL statements of above query will be :
1. SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
2. WHERE [t0].[EmpName] LIKE @p0
Notice that in this query "top 10" is missing since IEnumerable filters records on client side
IQueryable
1. IQueryable exists in System.Linq Namespace.
2. IQueryable can move forward only over a collection, it can’t move backward and between the items.
3. IQueryable is best to query data from out-memory (like remote database, service) collections.
4. While query data from database, IQueryable execute select query on server side with all filters.
5. IQueryable is suitable for LINQ to SQL queries.
6. IQueryable supports deferred execution.
7. IQueryable supports custom query using CreateQuery and Execute methods.
8. IQueryable support lazy loading. Hence it is suitable for paging like scenarios.
9. Extension methods supports by IQueryable takes expression objects means expression tree.
IQueryable Example
1. MyDataContext dc = new MyDataContext ();
2. IQueryable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
3. list = list.Take<Employee>(10);
Generated SQL statements of above query will be :
1. SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
2. WHERE [t0].[EmpName] LIKE @p0
Notice that in this query "top 10" is exist since IQueryable executes query in SQL server with all filters.
QuestionThank you Pin
Mahsa Hassankashi20-Sep-14 12:11
Mahsa Hassankashi20-Sep-14 12:11 
AnswerRe: Thank you Pin
Member 408341017-Jun-15 5:51
Member 408341017-Jun-15 5:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.