Linq Expand
Linq Expand
Tomas Petricek
[email protected]
The LINQ project (see [1]) is an extension to .NET Framework and most common
.NET languages (C# and VB.Net) that extends these languages with query operators
and some other features that make it possible to integrate queries in the languages. This
article requieres some previous knowledge of LINQ and C# 3, so I recomend looking at
the the specification at LINQ home page.
It compiles with no errors, but when you execute it DLINQ throws an exception
saying: "Static method System.Boolean MyTest(LINQTest.Nwind.Product) has no
supported translation to SQL." The exception is actually thrown when you try to fetch
results from q (for example using the foreach statement), because DLINQ attempts to
convert the expression trees to T-SQL only when the results are needed and the query
must be executed. To fix the example you can simply copy the code that checks whether
product name starts with "B" to the where clause of the query and it would work fine.
I'd say that checking whether query can be translated to T-SQL at runtime is
slightly against the DLINQ objective to catch all errors at compile time, but this is not
the aim of the article. Also the rules for writing correct queries are not difficult. You can
use all operators and some basic methods (like String.StartsWith) for working with
numbers and strings, but you can't call any other methods, particlularly methods that
you wrote.
2 The problem
The problem I wanted to solve is that you can't call your methods from queries. If
you have a more complex application with many similar queries it would be natural to
put the common subqueries to some function that is called by other queries. For
example you might want to write function that does some price calculations or function
that selects information about one significant product in category. If you look at some
questions in MSDN Forums you can see that I'm not the only one who is asking for this
(see [2]).
First I'll show a little example that demonstrates what you can do with the library I
wrote to keep your interest and than I'll explain the solution details. The following
query selects products from the Northwind database and performs calculation of price
for every product. Because we want to select only products that cost more than 30, the
price calculation is repeated twice in the query:
Using the extensions that I'll describe later you can extract price calculation to
lambda expression and use this expression in the query. Following code can be used for
querying both database and in-memory objects, because it is possible to translate the
lambda expression to T-SQL as well as execute it at runtime:
3 Implementation details
3.1 Expression expansion
As I mentioned, the crucial task for making the previous code work is replacing
calls to Invoke (extension) method with the actual expression tree of the used lambda
expression. This is done behind the scene by the wrapper created by ToExpandable in
the previous code, but you can do it directly as you can see in the following example:
// Declare 'calc' that will be used by other lambda expressions
Expression<Func<int, int>> calc =
i => i * 10;
The first declaration in this example creates expression tree for lambda expression
that multiplies its parameter by 10, later this expression can be used by other
expressions (like test that calls calc and adds 2 to the result). To use expression you
have to use Invoke extension method that is declared in the ExpressionExtensions
class (EeekSoft.Expressions namespace). This method is very simple, because it just
uses Compile method of the expression and executes the compiled delegate, but if you
write calc.Compile().Invoke(i) directly to the expression it is partially evaluated
while creating expression tree and it will not be possible to get expression tree of the
used lambda expression.
The expansion can be done by the Expand extension method that is also declared in
ExpressionExtensions. This is possible when you have variable of Expression or
Expression<F> type, where F is one of the Func delegates. The following example
demonstrates the expansion (some variables from previous example are used):
// You can use var because type is the same as the type of 'test'
var expanded2 = test.Expand();
You can see that using of Expand extension method is very simple. This example
also showed that you can use var keyword and let the compiler infer the type of
returned expression. This is possible because the type of returned expression is same as
the type of variable, on which the extension method is invoked or in other words as the
parameter passed to the method.
Now, let's examine the output printed in the previous example. The first line
represents the original expression before calling the Expand method. You can see that
expression calc is printed using syntax for lambda expressions as part of the test
expression. The important point here is that we didn't lose the expression tree of this
inner expression. The inner expression is followed by the call to Invoke method and it
is part of the Add expression that represents addition in the test.
The second line represents expression tree that is created by the Expand method.
You can see that inner lambda expression and call to the Invoke method was replaced
by its expression tree, which is multiplication applied to the parameter and the number
10. This representation can be later converted to T-SQL, because it doesn't contain any
calls to .NET methods that could not be translated. If you try to use non-expanded
expression the conversion will fail on the call to the Invoke method.
Question that could came to your mind is why do you have to call expression that
will later be replaced using the Invoke method instead of Compile().Invoke(..)
code that does exactly the same thing. It is because when C# 3 builds the expression tree
it executes the Compile method and the expression tree of the inner expression is be
replaced by delegate, so it would not be possible to access the original expression tree.
This is demonstrated by the following example:
The following output contains the delegate which is the result of Compile method
(embedded using value) instead of inner expression tree that is needed for
replacement:
i => Add(value(System.Query.Func`2
[System.Int32,System.Int32]).Invoke(i), 2)
You may be also wondering whether you could write (x => calc.Invoke(x) +
1).Expand() instead of using a variable for expression tree. If you try it, you'll get an
error message saying that "Operator '.' cannot be applied to operand of type
anonymous method." The problem here is that compiler needs some way to decide
whether lambda expression should be returned as a delegate or as an expression tree.
This depends on the type of variable to which lambda expression is assigned and in this
code it is not clear what should the type of return value be. Anyway you can expand
lambda expression if you call the Expand method as standard static method. In this case
compiler knows what the expected type of parameter is and it can decide whether it
should return delegate or expression tree:
This would be even more interesting if C# 3 were able to infer return type from the
type of lambda expression, but this is not possible in the current version, so you have to
specify type explicitly. In this case type arguments of method Expand are specified so
both type of method parameter and its return value are known. Because the return type
is known, it is also possible to use new var keyword for declaring variables with
infered type.
var q =
from p in db.Products.ToExpandable()
where calcPrice.Invoke(p) > 30.0m
select p;
In this query, the ToExpandable method is used for creating the wrapper around
DLINQ object that represents database table. When LINQ builds the expression tree
that represents the query, it uses CreateQuery method of this wrapper instead of
underlying DLINQ Table object. This is the trick that makes the expansion work,
because the wrapper simply calls Expand method described in the previous section and
than calls the CreateQuery of underlying DLINQ object. The returned value is
wrapped using new instance of the ExpandableWrapper class, because when building
queries that contain both where and select clauses, LINQ calls CreateQuery method
of the returned object again.
This is the implementation of CreateQuery method:
Expression<Func<Product,_>> selector =
p => new { p.ProductName, p.UnitPrice };
This can be partially solved by using method that returns Expression, because
method type parameters can be inferred, so you it wouldn't be needed to specify return
type explicitly. The problem is that in current preview type inference doesn't use
information from lambda expression that you pass as parameter to method so this can't
be done. The following code can't be currently compiled, but lets see whether this will
be fixed in the future releases:
The minor problem is that it is currently difficult to modify expression trees. There
is a class called ExpressionVisitor in the System.Query.dll, but it is currently
internal. This class makes modification of expression trees quite simple, so hopefully it
will be public in the future LINQ releases. For now, I used Reflector to extract this class,
because I didn't want to write the same class myself and I see no reasons why the class
shouldn't be public in the future.