What is IEnumerable, ICollection, IList, and IQueryable
Hi guys, I am always using that all, but I wasn't know what is exactly until a couple of days ago :)
Firstly let's separate these, queryable is, on the one hand, others in another hand. One is works as in database come from System.Linq and other is works in memory come from System.Collections.Generic.
Let’s assume you have 5000 records on your product table. When you create a query using LINQ,
_someRepository.GetAll();
it will respond as an IQueryable object with your all records. It creates a query like
SELECT *
FROM Product
You can filter this data on the database side
_someRepository.GetAll().Where(x => x.Name.Contains("can"));
it will respond as an IQueryable object with your all records. It creates a query like
SELECT *
FROM Product
WHERE Name like '%can%'
then you will achieve you IQueryable data, it means you get it from the database, until putting into a list.
var AllProduct = _someRepository.GetAll().ToList();
When you do that you will cast this to list which comes from Enumerable, you put that data on your ram.
IQueryable is database side and comes from linq, others are ram side comes from Collections. The first object type is enumerable which you can store on your ram.
Enumerable is a basic one, you cant manipulate the data, you cant remove one of them, you cant add, you can't replace, or you don't know how much there, But one thing you can do it, a basic loop. It is an enumerator, it has only 2 methods move next and reset.
If you go one stair up, you will see the collections. Which comes from ICollection, and is based on IEnumerable. As you can see the bottom picture, derived from IEnumerable
There are basic methods but enough for many thing, also you know the count :)
And the last one is a IList derived from Icollections and IEnumerable. You can do many things. Crud processes are of course there and some others
I will also add a List type here, a list derived from almost everything :) You can do add range, index, sort, etc. Also, you can run LINQ like where, select, select many, except, intersect etc. at lists the common class is Enumerable thus you can run
Happy Codes :)
Resources