Freigeben über


LINQ Farm Seed: Using the Expression Tree Visualizer

The Visual Studio 2008 CSharp samples include several valuable tools that LINQ developers can use to help expedite the development process. One of the is the Expression Tree Visualizer. This tool works in both Visual Studio Express and the other versions of Visual Studio that support C# development.

Note: If you are unfamiliar with expression trees, then you might want to view this post .

You can access the Expression Tree Visualizer by choosing Help | Samples from the Visual Studio 2008 menu system. From there you will be able to access links to the updated online version of the samples.

After you unzip the samples you should navigate to the LinqSamples directory, open the ExpressionTreeVisualizer project, and build it. By default, F6 is the build key in Visual Studio 2008.

After the build process is complete a library called ExpressionTreeVisualizer.dll will be created in the following directory:

...\LinqSamples\ExpressionTreeVisualizer\ExpressionTreeVisualizer\bin\Debug

Copy this DLL to your Visualizers directory. If the directory does not exist, you can create it inside the default user directory for Visual Studio. That directory would typically be located here:

...\Documents\Visual Studio 2008\Visualizers

One Windows XP system, the path might look like this:

...\My Documents\Visual Studio 2008\Visualizers

The Code Snippets, Projects and Templates directories also appear in this same location. You can also find the location of this directory by selecting Tools | Options | Projects and Solutions | General from the Visual Studio 2008 menu. You may need to restart Visual Studio after copying the ExpressionTreeVisualizer library to the Visualizers directory.

You can now test the visualizer by opening a project that creates an expression tree such as the Expression Tree Basics sample from the Code Gallery. Alternatively, you can create a default console application, add System.Linq.Expressions to the using statements, and add two lines of code to the program body:

 using System;
using System.Linq.Expressions;

namespace ConsoleApplication107
{
    class Program
    {
        static void Main(string[] args)
        {
            Expression<Func<int, int, int>> expression = (a, b) => a + b;

            Console.WriteLine(expression);
        }
    }
}

If you are using the code shown above, set a breakpoint on the WriteLine statement and run the program. Hover your mouse under the variable expression, as shown in Figure 1. A small Quick Info box with a magnifying glass will appear.

 

ExpressionTreeMagnifyingGlass

Figure 1: Examining the variable expression at run time in Visual Studio with Quick Info.

If you click on the magnifying class, an option to view the Expression Tree Visualizer will appear, as shown in Figure 2.

ExpressionTreeMagnifyingGlass2

Figure 2: By clicking on the magnifying class you can get access to the Expression Tree Visualizer.

If you select the visualizer option then it will appear and you can view the nodes of your expression tree, as shown in Figure 3.

ExpressionTreeMagnifyingGlass3

Figure 3: A visualization of the expression tree for a simple lambda expression.

If you are working with a LINQ to SQL query, then can use the same technique to view an expression tree. You will, however, need to take one extra step because the expression tree is a member of the IQueryable variable returned from a typical LINQ to SQL query expression. Consider this simple LINQ to SQL query:

 var query = from c in db.Customers
            where c.City == "London"
            select c;

At runtime, you can get Quick Info on the variable query, much as we did on the variable expression earlier in this post. Figure 4 shows how it looks:

ExpressionTreeMagnifyingGlass4

Figure 4: Hover the mouse over the variable query, then drill down to view the expression tree which is found in the queryExpression field. (Double click on this image to see a full-sized version.)

When you select the magnifying class, you will be presented with an option to pop up the Expression Tree Visualizer, which will look like the image shown in Figure 3, but with different data.

Summary

This post describes how to use the Expression Tree Visualizer that ships with the Visual Studio 2008 samples.

See also:

kick it on DotNetKicks.com

Comments

  • Anonymous
    February 13, 2008
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous
    February 13, 2008
    Thanks for this.... I really needed it :D

  • Anonymous
    February 17, 2008
    The comment has been removed

  • Anonymous
    February 17, 2008
    The comment has been removed

  • Anonymous
    February 17, 2008
    【原文地址】 Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET 【原文发表日期】 Sunday, February 17, 2008

  • Anonymous
    February 17, 2008
    IEnumerable Tales: Expression Tree Visualizer

  • Anonymous
    February 18, 2008
    I'm not a stupid man but I can't for the life of me find the updated samples you're talking about downloading.  How many clicks to get to the bottom of an msdn blog post?  The world may never know . . .

  • Anonymous
    February 18, 2008
    You have been kicked a good thing

  • Anonymous
    March 03, 2008
    To download the samples, you need to click on the "c# samples" link, and then click the 'Releases' tab. This is rather non-intuitive. At least, I was confused!

  • Anonymous
    March 05, 2008
    Is there a way to construct an expression tree from the string returned by .ToString() ? Thanks!

  • Anonymous
    April 18, 2008
    Welcome to the forty-first Community Convergence. The big news this week is that we have moved Future

  • Anonymous
    June 14, 2008
    The comment has been removed

  • Anonymous
    November 19, 2008
    I am as well interested in knowing of if there  is a way to construct an expression tree from the string returned by .ToString() ? in other words: I want to offer to user a UI that would allow them to construct a lambda expression string (e.g.: "y => y == '5'", in my case TResult of the Func would always be bool) then store it into a database as a business rule. How can i then reconstuct the corresponding Expression<Func<object, bool>> from my string? Thanks!

  • Anonymous
    November 29, 2008
    Taking the Magic out of Expression

  • Anonymous
    November 29, 2008
    Taking the Magic out of Expression

  • Anonymous
    December 09, 2008
    Declare your ObjectDataSource using lambda expressions

  • Anonymous
    March 07, 2009
    The comment has been removed

  • Anonymous
    April 26, 2009
    Hey, calvert is miss-guiding us. I placed it in the documentvs 2008...debugger... folder but every time i run it throws exception. But from Scott Gu's blog i found this location for the visualizers: Program FilesMicrosoft Visual Studio 9.0Common7PackagesDebuggerVisualizers Now its working. Thank you Calvert.

  • Anonymous
    September 19, 2009
    It appears that the current visualizer does not work with 2010 (beta 1). Is there a revised version for 2010? It appears to be missing from the C# samples for 2010 as well.

  • Anonymous
    August 28, 2010
    Thanks for the tip!  Installed the Visualiser on VS 2008, and it works a treat.

  • Anonymous
    September 10, 2011
    Can you give me new link for expression tree visualizer for vs2008? because i can't find the file