Pages

Wednesday, 4 May 2016

Some best practices that a c# developer should follow (3)

3. IEnumerable  Vs IQueryable:


The first important point to remember about “IQueryable” is that “IQueryable” interface inherits from “IEnumerable”, so whatever “IEnumerable” can do, “IQueryable” can also do.


 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
Suitable for LINQ to Object and LINQ to XML queries. LINQ to SQL queries.
Custom Query Doesn’t supports. Supports using CreateQuery and Execute methods.
Extension mehtod
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

IQueryable:

While querying data from database, IQueryableexecute select query on server side with all filters. Hence does less work and becomes fast.

The “IQueryable” is the interface that allows LINQ-to-SQL (LINQ.-to-anything really) to work. So if you further refine your query on an “IQueryable”, that query will be executed in the database, if possible.

IEnumerable :

While querying data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data. Hence does more work and becomes slow.

For the “IEnumerable” case, it will be LINQ-to-object, meaning that all objects will have to be loaded into memory from the database then the filter will be applied to it.


Now we can say that the main difference between “IQueryable” and “IEnumerable is about where the filter logic is executed. One executes on the client side and the other executes on the database.

Example:

So For more clarity we can consider an example where we have 10000 records for User in our database and let say only 900 out which are active users, so in this case if use “IEnumerable approach then first it load all 10000 records in the memory then apply the IsActive filter on it which eventually returns the 900 active users.(Two steps process and load all the records in memory as well which makes the process slow)
While on the other hand on the same case if we use “IQueryable” approach it will directly apply the IsActive filter on the database which directly from there will returns the 900 active users.(One step process and will return less records which will be much faster)
In this case and in all such case follow could be the approaches to use.

Not recommended approach

 
//IEnumerable 

       IEnumerable<User> Users = ...;
       var activeUsers = Users.Where(c => c.IsActive); 
 

Recommended approach


//IQueryable
 
       IQueryable<User> Users = ...;
       var activeUsers = Users.Where(c => c.IsActive);
 


Previous

5 comments:

  1. This blog giving the details of technology. This gives the details about working with the business processes and change the way. Here explains think different and work different then provide the better output. Thanks for this blog.
    HR Consultancy in Bangalore

    ReplyDelete
  2. This is awesome, thanks for explaining this in a very easy way, you are the best

    ReplyDelete