Whats the difference between IQuerable and IEnumerable [Resolved]

Posted by Swappy_Gaj under ASP.NET MVC on 1/24/2015 | Points: 10 | Views : 1560 | Status : [Member] | Replies : 2
Difference between the two
Real Life Example Use
When to use what Interface

Any Help Will be appreciated.


Posted by: Sheonarayan on: 1/24/2015 [Administrator] HonoraryPlatinum | Points: 50


Both are used to filter the records from the database. IQueryable ultimately inherits IEnumerable interface so all the feature of IEnumerable exists into IQueryable (like iteration).

However, the major difference is following considering below example.
IQueryable<PersonalDetail> list = db.PersonalDetails.Where(p => p.AutoId == 10);

IEnumerable<PersonalDetail> IIlist = db.PersonalDetails.Where(p => p.AutoId == 10);

In case of IQueryable, the sql statement is made to get only one record from database where AutoId is 10.
In case of IEnuerable, the sql statement is made to get all records from the database and then filter where AutoId is 10.

This gives us following conclusion.

1. IQueryable is good when you are retrieving and filtering the remote data such as getting data from database, web services etc. where as IEnuerable is good to get the data from in-memory data such as list or array that is created from C# code.

2. Both are readonly and forward only however IQueryable exists into System.Linq namespace but IEnuerable exists into System.Collections namespace.

3. IQueryable is used mainly when we need to LINQ to SQL or we need to work with any database objects with LINQ, IEnuerable is used where there is collection is enumeration is required.

I hope this helps.


Sheo Narayan

Swappy_Gaj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Swappy_Gaj on: 1/26/2015 [Member] Starter | Points: 25

Awesome Bro Thanks A Lot

Swappy_Gaj, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response