1.5. Expression Trees

LINQ can treat lambda expressions as data at run time. The type Expression<T> represents an expression tree that can be evaluated and changed at run time. It is an in-memory hierarchical data representation where each tree node is part of the entire query expression. There will be nodes representing the conditions, the left and right part of the expression, and so on.

Expression trees make it possible to customize the way LINQ works when it builds queries. For example, a database provider not supported natively by LINQ could provide libraries to translate LINQ expression trees into database queries.

Listing 1-5 shows how to represent a lambda expression with an expression tree.

Example 1-5. Using an Expression Tree
Expression<Func<Person, ...

Get LINQ for Visual C# 2008 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.