What are expression trees in LINQ?

What are expression trees in LINQ? Can someone point me to a resource with code and explanation?

+6
linq
source share
1 answer

An expression tree is a tree where all Expression nodes are (carry me).

I'm not quite sure if you are asking about a particular application of expression trees or not, so this has been a little delayed, but should give an idea of ​​general ways to use them. MSDN has some common concept of programming concepts for expression trees , and there is always documentation of the actual classes in System.Linq.Expressions ). I find these Microsoft provided examples useful when I first looked at expressions, but I don’t remember correctly.

You are probably familiar with the concept of a syntax tree. For example, 2 + 2 usually visually displayed with a syntax tree:

  + / \ 2 2 

Expression trees are very similar in concept. This is a smilar tree where all nodes are an expression. The 2 + 2 expression will be represented by code

 Expression.Add(Expression.Constant(2), Expression.Constant(2)) 

which returns a BinaryExpression and is an expression tree.


Lambda expressions

Expressions are also often used (when used) by lambda expressions.

This is a lambda expression or a lambda block:

 i => {return i * 2;} 

This is a lambda expression:

 i => i * 2 

You are probably used to seeing the following code of the following Func type:

 Func<int, int> timesTwo = i => i * 2; 

However, this is also true:

  Expression<Func<int, int>> timesTwoExpression = i => i * 2; 

In this case, the compiler will generate an expression tree for the expression, not the delegate. You can later call Compile() on it to get a delegate.

 Func<int, int> timesTwo = timesTwoExpression.Compile() 

However, since the expression has not yet been compiled, it can be checked and / or processed. For example, I can check the body of an expression and confirm that the multiplication constant is actually 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 personally used this to get property / method names , provide a zero elevator operator for a chain of member calls, and specify a type parameter for generic types when the type is known only at run time. Note. I cannot vouch for specific answers to these related questions, I did not test them against the code I wrote; I provide them for reference and example only.

This allows access to a limited part of the Expression API (you can only use it with lambda expressions, not with Lambda operators), but it can be very useful. However, this is an advanced feature, and your entire team may not understand it so that it responds responsibly.


Expressions for LINQ Query

Expressions are part of LINQ. However, LINQ includes many C # functions, including query syntax, lambdas, general delegates, and expressions.

Expressions are used in LINQ queries under covers to provide some IQueryable. This is where, for example, C # code (expressions) is rewritten and executed as SQL. You can start with How to Use Expression Trees to Create Dynamic Queries and Walkthrough. Creating an IQueryable LINQ Provider , but this is becoming quite problematic.


Expressions for implementing dynamic code / language

I understand that the Expression API is ultimately simple enough to represent most language constructs and serves as the basis for implementing dynamic / interpreted languages; although I never used it in that capacity. You can find more information about this use in materials related to Dynamic Language Runtime .

+2
source share

All Articles