Friday, September 29, 2017

C# IEnumerable vs IQueryable

The primary difference is that IEnumerable is great for working with in-memory collections, and IQueryable for a remote data source, like a database or web service.

IEnumerable : 
IEnumerable represent a forward collection of T and It has one GetEnumerater() method, from which you can iterate through collection.
  • It does not support adding, removing objects on collection.
  • It loads all object collection into local memory by using LINQ or entity framework before running query.
  • It is best option for in-memory data source like object collections, if your IEnumerable query is being executed against external database, it would be network overhead and first loads whole entity data in local memory and then apply filter.
 public interface IEnumerable<out T> : IEnumerable
    {      
        IEnumerator<T> GetEnumerator();

    }


IQueryable
 IQueryable is similar like LINQ and it parse the expression tree and generates SQL querya nd execute the script against the external data source.
  • It inherits the IEnumerable interface so it allows you to iterate over collection using ForEach statement.
  • It is best option for out-memory data source like database and it send only the filtered data to client and reduce network overhead due to reducing record count.

public interface IQueryable<out T> : IEnumerable<T>
{

}

No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...