LINQ Interview Question #9

What is IQueryable<T>?

LINQ Fundamentals, Syntax, IEnumerable & IQueryable Junior Beginner

Quick Interview Answer

IQueryable<T> represents a query that can be interpreted by a query provider. In addition to being enumerable, it exposes an expression tree describing the query.

Detailed Explanation

IQueryable<T> represents a query that can be interpreted by a query provider. In addition to being enumerable, it exposes an expression tree describing the query.

Providers such as Entity Framework Core inspect that expression tree and translate supported operations into another form, commonly SQL. The query is normally executed only when it is enumerated or a terminal operator is called.

Code Example

IQueryable<Product> query = db.Products
    .Where(p => p.IsActive)
    .OrderBy(p => p.Name);

// SQL is normally sent when the query is executed.
var products = await query.ToListAsync();