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