IEnumerator

 

 

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.

Traversal:

  • It is a forward only collection.

Returns:

  • 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.

Namespace:

  • System.Collection

Derives from:

  • No base interface

Processing Items:

  • IEnumerable can move forward only over a collection, it can’t move backward and between the items.

Moving Items:

  • Only supports moving to next item.

Searching:

  • It will process all records one by one in forward direction, will be done.

Filtering:

Indexing:

  • 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.

Performance:

  • Slower that IQueryable

When to use:

  • When querying data from in-memory collections like List, Array, etc.

Deferred Execution :

Custom Query:

Lazy Loading:

  • IEnumerable doesn’t support lazy loading. Hence not suitable for paging like scenarios.

Extension method:

  • Extension methods supports by IEnumerable takes functional objects.

Usage :

MyDataBaseContext dbC = new MyDataBaseContext ();
IEnumerable list = dbC.Employees.Where(p => p.Name.StartsWith("T"));
list = list.Take(10);

 

SQL Query:

 

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.