Subexpressions
When you’re writing a query, you may find that an operation over an associated collection comes up repeatedly. For example, you may want to perform a calculation over the collection, and both order and filter by the result of this calculation. To avoid retyping the common expressions each time – and having the database engine re‑evaluate them each time – you can define subexpressions for them, then use these subexpressions as if they were actual attributes of the entity.
To define a subexpression, add it to the Query.Subexpressions collection. You must specify the name of the subexpression, the query expression it encapsulates, and the column in the target table on which to join to the main table.
Defining a subexpression |
query.Subexpressions.Add( |
Once a subexpression is defined, you can use it by specifying its name. You can use a subexpression name anywhere you would normally use a field name – typically in a query expression, sort order or projection.
Using subexpressions in a query |
Query query = new Query(typeof(Client)); |
In LINQ, you can create sub expressions using the let keyword; no special APIs are required.