Welcome to MSDN Blogs Sign in | Join | Help

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
Published Wednesday, February 13, 2008 10:54 AM by Charlie Calvert
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# LINQ Farm Seed: Using the Expression Tree Visualizer

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Wednesday, February 13, 2008 1:55 PM by DotNetKicks.com

# re: LINQ Farm Seed: Using the Expression Tree Visualizer

Thanks for this.... I really needed it :D

Thursday, February 14, 2008 5:25 AM by marlongrech

# Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET

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

Sunday, February 17, 2008 2:20 PM by ASP.NET AJAX Team Blogs

# Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET

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

Sunday, February 17, 2008 2:29 PM by BusinessRx Reading List

# 2月17日链接篇: ASP.NET, ASP.NET AJAX, Visual Studio, .NET

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

Sunday, February 17, 2008 5:11 PM by Joycode@Ab110.com

# IEnumerable Tales: Expression Tree Visualizer

IEnumerable Tales: Expression Tree Visualizer

Monday, February 18, 2008 4:28 AM by di .NET e di altre Amenit

# re: LINQ Farm Seed: Using the Expression Tree Visualizer

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

Monday, February 18, 2008 4:17 PM by skain

# re: LINQ Farm Seed: Using the Expression Tree Visualizer

You have been kicked a good thing

Tuesday, February 19, 2008 6:04 AM by Razan

# re: LINQ Farm Seed: Using the Expression Tree Visualizer

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!

Monday, March 03, 2008 12:12 PM by Steve

# construct expression tree from string?

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

Thanks!

Wednesday, March 05, 2008 11:29 AM by Kirk

# Community Convergence XLI

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

Saturday, April 19, 2008 12:04 AM by Charlie Calvert's Community Blog

# Danger propecia.

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

Saturday, June 14, 2008 10:16 AM by Propecia.

# construct expression tree from string?

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!

Wednesday, November 19, 2008 8:26 AM by glebars20@hotmail.com

# Taking the Magic out of Expression<T>

Taking the Magic out of Expression

Saturday, November 29, 2008 4:50 PM by CodeThinked

# Taking the Magic out of Expression

Taking the Magic out of Expression

Saturday, November 29, 2008 4:53 PM by CodeThinked

# Declare your ObjectDataSource using lambda expressions

Declare your ObjectDataSource using lambda expressions

Tuesday, December 09, 2008 10:59 AM by LavaBlast Software Blog

# re: LINQ Farm Seed: Using the Expression Tree Visualizer

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

Saturday, March 07, 2009 12:53 PM by Waqas

# re: LINQ Farm Seed: Using the Expression Tree Visualizer

Hey, calvert is miss-guiding us. I placed it in the document\vs 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 Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\Visualizers\

Now its working. Thank you Calvert.

Sunday, April 26, 2009 2:24 PM by Waqas

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker