Skip to content

IEnumerable vs. IQueryable

Question

Describe the difference between IEnumerable and IQueryable.

Short interview answer

IEnumerable<T> represents in-memory enumeration using delegates. IQueryable<T> carries an expression tree that a provider can translate, for example into a database query.

Detailed answer

The distinction matters when composing queries against a remote provider:

IQueryable<Customer> query = provider.Customers;
var names = query
    .Where(customer => customer.IsActive)
    .Select(customer => customer.Name)
    .ToList();

The provider can translate the expression tree before ToList materializes results. Calling an arbitrary local method inside Where may not be translatable. Keep filtering and projection in IQueryable<T> while translation is desired, then materialize deliberately before using arbitrary in-memory code.

Sources