Описание тега linq-expressions

An API for composing code with declarative expression trees, used by LINQ and the DLR.

About

The System.Linq.Expressions namespace in.NET 3.5 and later provides an API for composing code in terms of declarative expression trees, as well as a compiler to transform top-level lambda expressions into delegates (i.e., callbacks). Originally created to facilitate the creation of custom query providers in linq, it has since evolved to form the basis of the /questions/tagged/dynamic-language-runtime.

Relationship to LINQ

The first version of System.Linq.Expressions, which shipped with.NET 3.5, was designed to model query expressions. Consequently, it supports only a subset of the expression types representable in.NET languages like C# and VB.NET. Query expressions in LINQ are processed by a query provider and, ideally, transformed into a combination of local and remote operations. The LINQ to SQL and LINQ to Entity Framework providers, for example, will attempt to transform a query expression into equivalent SQL, enabling operations like filtering, sorting, and aggregation to be performed (and optimized) by the database engine. This is particularly useful for larger data sets that would render in-memory processing impractical.

Evolution

While the System.Linq.Expressions APIs proved useful for modeling complex expressions, it lacked the control flow mechanisms necessary to model the kind of code produced by imperative programming languages. Thus, it was less useful for general-purpose code generation. This changed when Microsoft began developing its Dynamic Language Runtime (DLR), a set of common language services designed to support the development of dynamic languages.

Core aspects of the DLR include support for dynamic call sites, dynamic code generation, and language hosting. Microsoft decided to use System.Linq.Expressions as the code model for the DLR's dynamic call site and code generation infrastructure. The expression tree API and compiler were extended to support a much wider range of language constructs, most notably control flow (conditions, loops, switch statements, etc.) and exception handling. Languages like IronRuby and IronPython ultimately transform their own syntax trees into LINQ expression trees, which may then be compiled or interpreted directly by the DLR.

Developers wishing to perform runtime code generation may choose to compose code using LINQ/DLR expression trees as a more convenient and less error-prone alternative to emitting raw IL byte code.