What are Expression Trees in LINQ? Could someone point me to a resource that has code and explanation?
An expression tree is a tree where all the nodes are Expression
s (bear with me).
I am not entirely sure whether you are asking about a specific application of Expressions trees or not, so this has gotten a bit lengthy, but should give an idea of general ways they can be used. MSDN has some general programming concepts documentation for Expression Trees and there is always documentation of the actual classes in System.Linq.Expressions
). I believe I found these Microsoft provided examples useful when I first looked at Expressions but I do not rightly recall.
You are probably familiar with the concept of a syntax tree. For example, 2 + 2
would usually be visually depicted of having the syntax tree:
+
/ \
2 2
Expression trees are very similar in concept. The are a smilar tree where all nodes are an expression. The expression 2 + 2
would be represented by the code
Expression.Add(Expression.Constant(2), Expression.Constant(2))
which is returns a BinaryExpression
and is an an expression tree.
Expressions are also often utilized (when they are utilized) by Lambda Expressions.
This is a lambda statement or lambda block:
i => {return i * 2;}
This is a lambda expression:
i => i * 2
You are probably used to seeing the above code next having the type Func
:
Func<int, int> timesTwo = i => i * 2;
However it is also valid to do:
Expression<Func<int, int>> timesTwoExpression = i => i * 2;
In this case the compiler will emit an expression tree for the expression rather than the delegate. You can later call Compile()
on it to get the delegate.
Func<int, int> timesTwo = timesTwoExpression.Compile()
However, because the expression is not yet compiled it can be inspected and/or manipulated. For example, I can inspect the body of the expression and confirm that the multiplication constant is in fact two.
Expression<Func<int, int>> timesTwoExpression = i => i*2;
Assert.True(timesTwoExpression.Body.NodeType == ExpressionType.Multiply);
Expression right = ((BinaryExpression) timesTwoExpression.Body).Right;
Assert.True( right.NodeType == ExpressionType.Constant);
Assert.AreEqual( ((ConstantExpression) right).Value, 2);
I have personally used this to get property/method names, provide a null lift operator for chaining member calls, and to specify type parameter on generic types when the type is only known at run time. Note: I cannot vouch for the particular answers on those linked questions, I have not checked them against the code I wrote; I provide them only for reference and example.
This only allows access to a limited portion of the Expression API (you can only can only use it with Lambda expressions and not Lambda statements) but it still can be very useful. However, it is an advanced feature and all your team may not understand it so use responsibly.
Expressions are part of LINQ. However LINQ encompasses quite a few C# features including query syntax, lambdas, generic delegates, and expressions.
Expressions are used by LINQ Queries under the covers for providing some of the behavior related to IQueryable.
That is where, for example, C# code (expressions) are rewritten and executed as SQL. You might start with How to: Use Expression Trees to Build Dynamic Queries and Walkthrough: Creating an IQueryable LINQ Provider but this gets rather involved.
I understand the Expression API is ultimately robust enough to represent most language constructs and serves as a foundation for implementing dynamic/interpreted languages; though I have never used it in this capacity. You may find more information on this use in materials related to the Dynamic Language Runtime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With