IEnumerable Vs IQueryable Vs IList

Sudhanshu Jain
3 min readJul 7, 2020

--

This is the most common question always asked in the interview, so today we are going to discuss about what they are and their differences.

IEnumerable :
It is one of the interface which resides in System.Collections namespace and has only one method as GetEnumerator(), which is useful for traversing the collections. This Method returns the IEnumerator which has property as Current along with the methods as MoveNext() and Reset().

IList :
It is one of the important interface which resides in System.Collections.Generic namespace along with that it is inheriting the IEnumerable. It is performing one of the important operations as Inserting and removing elements from the collections.

IQueryable :
It is another interface which resides in System.Linq namespace and useful as State Provider. The results of that query can be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed and it is also inheriting the IEnumerable.

Now you might be wondering that why IQueryable is from different namespace because IQueryable is the only interface which provides the power to write and query the expression of database. Linq provides the mechanism to connect through the database and performing complex operations behind the scene.

There are various databases in the market such as MySql, Sql Server, Oracle, MongoDb etc. To get through all these kinds of databases, the various teams has written their own methodology to connect and query the database using this interface and which makes LINQ more generic enough for the database system.

In what Scenarios we should use what IEnumerable or IQueryable ?

IEnumerable is useful for IN Memory operations like List, Arrays e.t.c. so whenever you are querying the database, it will perform the select operations in the db and later filter in the memory like below :

IEnumerable Series

And the result is :

LINQ GENERATED SQL
IEnumerable Filtration Process

As you can see that using IEnumerable, it fetches all the data first from database and then filter the data as in memory collection, which directly says, that it filter the data in memory.

IEnumerable is good enough if you want to use the operations in memory collections but it will not be beneficial if you are using directly with the database.

IQueryable is useful when you want to use the operations with remote database and include all the filters while querying the database.

IQueryable Series
LINQ Generated SQL

As you can see, it include the filter condition and produces the required SQL and when you are working with heavy databases, it improves the performance of the application as well. IQueryable is useful for OUT Memory Collections.

IQueryable Filtration Process

How to use both ?

The solution is that, you have to use both the interfaces for the optimised application. IQueryable helps you to fetch the data from the database and IEnumerable helps you for the computation in the memory.
In this way, we will achieve the best of both worlds.

That’s all for now, In the next blog, we will see that how to improve the performance of the application and reduce the loading time of the page.

…Hit Claps if you like this blog…

--

--

Sudhanshu Jain
Sudhanshu Jain

Written by Sudhanshu Jain

Senior Software Engineer | Scrum Master Certified | Blogger | Tech Mentor

No responses yet