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 (21)

  1. You’ve been kicked (a good thing) – Trackback from DotNetKicks.com

  2. marlongrech says:

    Thanks for this…. I really needed it ๐Ÿ˜€

  3. Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  4. Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  5. ใ€ๅŽŸๆ–‡ๅœฐๅ€ใ€‘ Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET ใ€ๅŽŸๆ–‡ๅ‘่กจๆ—ฅๆœŸใ€‘ Sunday, February 17, 2008

  6. IEnumerable Tales: Expression Tree Visualizer

  7. skain says:

    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 . . .

  8. Razan says:

    You have been kicked a good thing

  9. Steve says:

    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!

  10. Kirk says:

    Is there a way to construct an expression tree from the string returned by .ToString() ?

    Thanks!

  11. Welcome to the forty-first Community Convergence. The big news this week is that we have moved Future

  12. Propecia. says:

    Propecia and online drugs stores. Propecia side effects fre. Propecia reverse impotence. Propecia post side effect. Propecia uk order online.

  13. glebars20@hotmail.com says:

    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!

  14. CodeThinked says:

    Taking the Magic out of Expression

  15. CodeThinked says:

    Taking the Magic out of Expression

  16. Declare your ObjectDataSource using lambda expressions

  17. Waqas says:

    Hi Chalie, I get this exception whenever I click on the magnifier:

    Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.

    And no window is shown which show the expression tree. Thanx

  18. Waqas says:

    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.

  19. Jim Wooley says:

    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.

  20. Thanks for the tip!  Installed the Visualiser on VS 2008, and it works a treat.

  21. sofyan says:

    Can you give me new link for expression tree visualizer for vs2008? because i can't find the file