LINQ Interview Question #101

What is an expression tree in LINQ?

LINQ Performance, EF Core LINQ, Expressions & Advanced Queries Senior Advanced

Quick Interview Answer

An expression tree represents code as a tree of expression objects rather than immediately compiled executable instructions. In LINQ, expression trees are especially important with IQueryable<T> because a query provider can inspect the tree and translate supported operations into another language such as SQL.

Detailed Explanation

An expression tree represents code as a tree of expression objects rather than immediately compiled executable instructions. In LINQ, expression trees are especially important with IQueryable<T> because a query provider can inspect the tree and translate supported operations into another language such as SQL.

Expression trees commonly use Expression<TDelegate>. They allow providers to understand the structure of predicates, projections, and other query operations.

Code Example

Expression<Func<Product, bool>> filter =
    p => p.Price > 100;

IQueryable<Product> query =
    db.Products.Where(filter);