I am really glad that Scott Guthrie made the  LINQ to SQL Visualizer available since it has helped me tremendously figure out what is wrong with my LINQ queries. One of the things that really annoyed me about it however, was when you check Original Query it shows you the SQL query that it generated, and it shows a list of the parameters at the bottom. It is nice to see what the parameters are and their values but umm, small problem, what if I wanted to copy and paste the query into SQL Server Management Studio to run it there? Well, you can do it, but you have to scroll forever highlighting the code, and you have to take the parameter list at the bottom and refactor it as DECLARE and SET statements to get a runnable query! Not quite sure why someone didn’t go the extra mile and do this. Luckily the source code is available with the download from Scott's blog,  because I was able to implement this feature in less than 30 minutes.

As you can see in the screen shot below, when “Original Query” check box is checked, it now generates a complete script that can literally be copied and pasted and ran within SQL Server Management Studio. I also added a “Copy to Clipboard” button so you don’t have to scroll any longer highlighting the text.

image

The only thing is, currently int, bigint, bit, datetime, and varchar’s are ONLY supported in the generation of  the parameter declaration since that was all I was intent on doing to solve the problem at hand before I got side tracked on this thing.

Download the source code here. The only modifications to this code were for these small changes, otherwise the rest of the credit goes to Scott Guthrie and pals.

Enjoy!