Its Interface supports a simple iteration over a non-generic collection.
Best suitable for:
- In-memory traversal
- Querying data from in-memory collections like List, Array, etc.
- local queries
- for LINQ to Object and LINQ to XML queries. As long as you have something that implements IEnumerable<T>, you can use LINQ.
- It is a forward only collection.
- A list that is the actual querying took place and you get the results. ToList is issued to force running the query and returning these enumerable results...
When to use:
- Use IEnumerable/ToList if you want to return a list that has been retrieved from the Database.
- No base interface
- IEnumerable can move forward only over a collection, it can’t move backward and between the items.
- Only supports moving to next item.
- It will process all records one by one in forward direction, will be done.
- IEnumerable filters records on client side.
- Indexing is provided by default.
How it works:
- While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data.
- Slower that IQueryable
When to use:
- When querying data from in-memory collections like List, Array, etc.
Deferred Execution :
- IEnumerable supports deferred execution.
- IEnumerable doesn’t supports custom query.
- IEnumerable doesn’t support lazy loading. Hence not suitable for paging like scenarios.
- Extension methods supports by IEnumerable takes functional objects.
MyDataBaseContext dbC = new MyDataBaseContext ();
IEnumerable list = dbC.Employees.Where(p => p.Name.StartsWith("T"));
list = list.Take(10);
SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0 //@p0 represents 'T%'
Whatever IEnumerable can do, IQueryable can also do.