Understanding and Using LINQ Expression Trees - Expression Tree Elements (
Page 3 of 3 )
Viewing the Expression Tree Elements
At some point, you’ll want to look at the expression trees you create or those created by other developers. The tree structure is relatively easy to coax out of the expression tree. Listing 6 shows an example of how you might work with an expression tree and the decomposition of that tree.
Listing 6: Creating an Expression Tree
private void btnTest_Click(object sender, EventArgs e)
{
// Create the expression tree.
Expression<Func<String, bool>> ShortString =
MyString => MyString.Length <= 3;
// Obtain the expression tree elements.
// Contains the list of parameters.
ParameterExpression Parameters =
(ParameterExpression)ShortString.Parameters[0];
// Contains the evaluation.
BinaryExpression TheEvaluation =
(BinaryExpression)ShortString.Body;
// Contains the left side of the evaluation.
MemberExpression LeftSide =
(MemberExpression)TheEvaluation.Left;
// Contains the right side of the evaluation.
ConstantExpression RightSide =
(ConstantExpression)TheEvaluation.Right;
// Output the elements of the expression tree.
txtResult.Text = "The tree contains:\r\nParameters: " +
Parameters.ToString() + "\r\nEvaluation: " +
TheEvaluation.ToString() + "\r\nLeft Side: " +
LeftSide.ToString() + "\r\nRight Side: " +
RightSide.ToString();
}
ADVERTISEMENT
As before, the example begins by building an expression tree. The next section of code takes the expression tree apart. It begins with the parameter, MyString. Next comes the expression, MyString.Length > 3. This expression contains a left side, MyString.Length, and a right side, 3. All four of these elements have specific types:
Figure 1 shows the output from this program. If this expression tree contained additional elements, you could continue to move down the tree until you’d looked at every element. The point is that the output from this example will show you a complete expression tree. You can also view all of this information using the debugger.
Figure 1: Expression trees save your queries in a tree-formatted object.
You don't normally need to know whether a data source uses expression trees because it's the responsibility of the data provider vendor to determine whether the data provider uses expression trees. Because expression trees don't have much of an affect on your daily LINQ usage, they don't appear anywhere else in this article. If you want this same functionality as a visualizer, check out the “LINQ Query Visualizer Sample” article at http://msdn.microsoft.com/en-us/library/bb629285.aspx for C# developers and http://msdn.microsoft.com/en-us/library/bb882632.aspx for Visual Basic.NET developers.
Bottom Line
Expression trees are an essential part of your developer toolbox when using LINQ. In most cases, the provider creates the expression tree for you, but it’s nice to be able to create one from bits and pieces as needed. Using the techniques in this article, you can create extremely complex expression trees to interact with data at a low level. The important thing to remember is to build your tree in layers so that you can test one layer at a time. You should also build a diagram of your lambda expression so that you don’t get lost in the myriad object constructions required to create a custom expression tree.
BIO
John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 80 books and over 300 articles to date. The topics range from networking to artificial intelligence and from database management to heads down programming. His current project is LINQ for Dummies (scheduled for publication in August 2008), which you can preorder at http://www.amazon.com/exec/obidos/ASIN/0470277947/datacservip0f-20/. His technical editing skills have helped over 56 authors refine the content of their manuscripts. You can reach John on the Internet at JMueller@mwt.net and his Web site at: http://www.mwt.net/~jmueller/.
vider creates the expression tree for you, but it’s nice to be able to create one from bits and pieces as needed. Using the techniques in this article, you can create extremely complex expression trees to interact with data at a low level. The important thing to remember is to build your tree in layers so that you can test one layer at a time. You should also build a diagram of your lambda expression so that you don’t get lost in the myriad object constructions required to create a custom expression tree.
BIO
John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 80 books and over 300 articles to date. The topics range from networking to artificial intelligence and from database management to heads down programming. His current project is LINQ for Dummies (scheduled for publication in August 2008), which you can preorder at http://www.amazon.com/exec/obidos/ASIN/0470277947/datacservip0f-20/. His technical editing skills have helped over 56 authors refine the content of their manuscripts. You can reach John on the Internet at JMueller@mwt.net and his Web site at: http://www.mwt.net/~jmueller/.