Linq to Sql Query Visualizer

Scott Guthrie described an awesome Linq to Sql Query Visualizer here.  His context is Visual Studio 2008 Beta 2.  Since then, it’s gotten (almost) baked into Visual Studio 2008 RTM.  The functionality is the same, installing it is slightly different.

What is it:

During debugging, show the actual sql that will get executed from your linq query, and allow you to execute the query and see the results – all within a debugger visualizer.

How to get it:

  1. Navigate to C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033  (Modify to suite if you installed Visual Studio 2008 to a different directory.)

  2. unzip

  3. Navigate inside the unzipped content to LinqSamples/QueryVisualizer

  4. Build the project if necessary.  I chose to change the solution to Release mode, since I likely won’t be debugging the visualizer.  :)

  5. Copy SqlServerQueryVisualizer\bin\Release\LinqToSqlQueryVisualizer.dll  to C:\Users\<your name here>\Documents\Visual Studio 2008\Visualizers (or in XP copy it to C:\Documents and Settings\<your name here>\My Documents\Visual Studio 2008\Visualizers)

  6. Restart Visual Studio 2008

How to use it:

(graphics pillaged from Scott Gu’s original post)

  1. Create a Linq to Sql query

  2. Set a break point after the creation of the Linq to Sql query is formed and before the results are used – e.g. before the sql is executed.

  3. Mouse over the results variable:

  4. Click on the magnifying glass:

Welcome to the Linq to Sql Query Visualizer.

Note the “Original query” checkbox on the bottom-left – if you click it, you’ll see how the query is actually parameterized correctly – no sql injection here.

Note the “Execute” button on the bottom-right – if you click it, you’ll see the results that will get returned into the IEnumerable<T> results var.

In many discussions since, I’ve heard people describe TSql constructs and syntax they’ve learned by watching the queries in this window.  I can’t wait … except now that I have Linq to Sql (and Linq to Entities on the horizon), why would I want to code in TSql anymore?  :)