IEnumerable vs. IQueryable: the differences?

There are some differences between IEnumerable vs. IQueryable that any coders need to know.

IEnumerable

  • IEnumerable is in the System.Collections namespace.
  • IEnumerable can browse elements only one way forward, it cannot browse back and forth between elements.
  • IEnumerable is best when querying from an in-memory collection ie in RAM like List, Array …
  • When querying data from the database, IEnumerable executes the select statement on the server and then downloads all data about the client before filtering the data.
  • IEnumerable matches Linq to Object and Linq to XML.
  • IEnumerable does not support custom queries.
  • IEnumerable does not support lazy loading so it is not suitable for pagination.

IQueryable

  • IQueryable is in the System.Linq namespace
  • IQueryable can only move one way forward in the collection, it cannot move back.
  • IQueryable is best for querying out-memory data as a database.
  • When querying, IQueryable executes the query and filters the data on the Server
    IQueryable is suitable for Linq to SQL.
  • IQueryable supports custom queries using the CreateQuery and Execute methods.
  • IQueryable supports lazy loading. So it is suitable for paging cases. Examples of IQueryable