Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท LINQ

The IQueryable<T> interface

Reference for IQueryable<T> in C#: how it differs from IEnumerable<T>, expression trees and providers, AsQueryable(), composing queries and common mistakes.

IQueryable<T> represents a query against a data source that is not necessarily in memory. It looks like IEnumerable<T> and exposes the same LINQ operators, but its operators take Expression<Func<...>> arguments instead of plain delegates. Each call adds a node to an expression tree. When the query is enumerated, a query provider (IQueryProvider) reads the whole tree and translates it, for example into SQL.

Database libraries are the main users, but the interface itself is just three members: ElementType, Expression and Provider. You see it daily as the type returned by DbSet<T> in Entity Framework, and you can wrap an in-memory collection with AsQueryable() to test code that expects an IQueryable<T>.

C#
IQueryable<User> adults = users.AsQueryable().Where(u => u.Age >= 18);
Console.WriteLine(adults.Expression);

Try it

C# A query is an expression until it runs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Compose a query step by step

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public interface IQueryable : IEnumerable
{
    Type ElementType { get; }
    Expression Expression { get; }
    IQueryProvider Provider { get; }
}

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

public static IQueryable<TElement> AsQueryable<TElement>(this IEnumerable<TElement> source);
Member Description
ElementType The element type T.
Expression The expression tree describing the query so far.
Provider The IQueryProvider that creates new queries and executes them.
AsQueryable() Wraps an IEnumerable<T> in an in-memory EnumerableQuery<T>. If the source is already queryable it is returned unchanged.

Returns: operators such as Where() and Select() return a new IQueryable<T> whose expression has one more node. Operators such as Count() and First() execute the query and return a value.

Errors: a provider throws if it cannot translate part of the tree (for example a call to your own method inside Where()). That happens at execution time, not when you write the query.

Usage notes

  • Use the right type in signatures. If a method takes IEnumerable<T> and you pass an IQueryable<T>, later operators run in memory via delegates. Keep database filters on IQueryable<T> so they are translated.
  • AsEnumerable() switches sides. Everything before it is translated by the provider, everything after runs in memory. Use it deliberately, for example to call a C# method the provider cannot translate.
  • Deferred execution. A query runs when enumerated, and again on every enumeration. Materialize with ToList() if you iterate twice.
  • Lambdas are trees. Only expression lambdas work; statement lambdas cannot be converted to Expression<T>. See expression trees.
  • Captured variables become parameters or constants in the tree, which is why b.Genre == genre can be sent to a database as a parameter.
  • Providers differ. Each provider supports a subset of operators and methods. AsQueryable() over a list supports everything because it simply compiles the tree, which makes it a poor test for provider-specific translation.
  • Return IQueryable<T> from repositories sparingly. It leaks query composition to callers and ties them to the provider. Many teams return IReadOnlyList<T> or a materialized page instead.
  • Dispose-related errors. Enumerating an IQueryable<T> after its owning context is disposed fails; materialize inside the scope.
  • Ordering rules. OrderBy() returns IOrderedQueryable<T> so ThenBy() can follow, just like in memory.