Home / Programming / Blog article: View T-SQL Query Generated By LINQ To SQL

| RSS

View T-SQL Query Generated By LINQ To SQL

September 22nd, 2008 | 5 Comments | Posted in Programming

While working with LINQ To SQL there are times when you are interested in looking at the query which will be executed on the database.  This post will cover different ways you can capture the query generated by LINQ To SQL.

SQL Server Profiler

SQL Profile has long been a preferred tool to capture activity on a database. By running SQL Server Profiler you can easily view the queries which are submitted to database.

image

Output to Console Window

If you are like me and have your output window always displayed then you can write your queries to output windows using these simple techniques.


context.Log = Console.Out;

will print all queries to console if you are working on a console application or to Output Window of Visual Studio. I usually put this statement within #if DEBUG to see all generated queries.

In case you only want to write a specific query to console, you can use a statement like this.

Console.WriteLine(context.GetCommand(query).CommandText);
Using Debug Visualizer

Visual Studio provides a Visualizer which will also show you the generated query. You can use it like any other Visualizer by placing your mouse pointer over the query variable.

image

Summary

In daily practice I output all queries to Output Window. It allows me to quickly get a feel of number of queries my application is firing and I can also examine each one of them individually.

kick it on DotNetKicks.com

Leave a Reply 9392 views, 2 so far today |
Tags:
Follow Discussion

5 Responses to “View T-SQL Query Generated By LINQ To SQL”

  1. curious Says:

    just a though, wouldn’t it possible to have such a feature in linqpad?
    http://www.linqpad.net/

  2. Deepak Says:

    You are right. Linqpad can also be used to view T-SQL queries generated by LINQ To SQL. I should have included it as another option. I have posted it here:

    http://www.thereforesystems.com/view-t-sql-query-generated-by-linq-to-sql-using-linqpad/

  3. @shish daga Says:

    Deepak,
    Tusi tum gr8 ho … good job .. keep it up.

  4. Cristiano Martins Says:

    How I can see the T-SQL Genereted by Linq to SQL where using the CompiledQuery.compile

Trackbacks

  1. View T-SQL Query Generated By LINQ To SQL Using Linqpad | One .Net Way  

Leave a Reply





Switch to our mobile site