Call Us at +91 95997 20600 | +1 (213) 814-4265|sales@codestoresolutions.com

Difference between IEnumerable, IQueryable and IList

Difference between IEnumerable, IQueryable and IList

For every backend developer, it is very important to have knowledge about IEnumerable, IQueryable, and IList. A programmer should know the difference between these and most important is, knowledge of how and where to use these.

In this article, I am trying to give explain these with example. So let’s start digging into these J

IEnumerable, IQueryable, and IList are interfaces in .net framework that can use as a data container. These all could be non-generic interface and generic type-safe interface.

Non-Generic – IEnumerable, IQueryable and IList

Generic type-safe Interface :- IEnumerable, IQueryable and IList

IEnumerable :- IEnumerable is a data container that is used to store data. We can perform “foreach” loop and other operations on this. IEnumerable gives only read-only access over stored data. We cannot perform add/delete item operation on IEnumerable.   IEnumerable brings all data from the database and then perform a filter on it. Let’s take an example to prove this. See below code

In above code, we have one table named “Orders” and we have a query to get all records from orders table using EF code first approach. In first line, a SQL query is created to get records from Order table and store that in IEnumerable type variable. But point to notice here is, SQL query still not executed on server. Query will only be fire on server when “foreach” loop execute on variable. In second line, I just filter records according to “CustomerID”. But IEnumerable type variable first fetch all records from server and brings them to client-side and then perform filter operation. To prove this I run SQL profiler. You can see SQL Script created by IEnumerable type variable.

SQL Profiler

As you can see in above snapshots, in first picture query execute on server when control reached in “foreach” loop. In second picture you can see SQL query (without where clause) that is created by IEnumerable. So it brings all data from server side to client side and then perform filter.

IQueryable :- IQueryable is same as IEnumerable but with additional facility and feature to load only requested data from Server side to client side. IQueryable is useful when have to deal with huge data. Unlike the IEnumerable, IQueryable filter data at server side and brings only filtered records from server to client. Let’s have an example for this also.

SQL Profiler

In above code, you can see, IQueryable creates SQL script with filter (with where clause) and when control reached at “foreach” loop (IQueryable variable called to get data) then script executed at server and returns filter data only.

IList:-IList is combination of IEnumerable and IQueryable with addition feature to add/delete items from list. IList gives full controller over stored data. Unlike IEnumerable and IQueryable, we can add delete items from list. Also we can get index based data from list. Have look at below example for better understanding.

In above picture, Get all records from database and store in results i.e. IList type variable. Here we have 3 records.

Now, let’s add one new record in result set.

As you can see in above picture one new record is added in result set.

Now let’s remove an item from result set.

In above code, I remove an item from result set.  We can see in above picture now we have only 3 records in result set.

Hope this blog helped you to understand the difference between IEnumerable, IQueryable and IList. Now you can decide where and what to use from these according to requirement and need.

Please feel free to approach us for any question regarding this. We will answer you for that.

Bye for Now.

Enjoy Coding.

Author

Ajay KumarAt CodeStore, our team leverages my expertise in full-stack engineering—specializing in .NET, NodeJS, AngularJS, and more—to solve complex problems and drive technological innovation. As CEO, I've been instrumental in advising businesses on digital strategies, embodying a commitment to successful technology integration.
Go to Top