|
View T-SQL Query Generated By LINQ To SQL
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.

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.
![]()
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.
5 Responses to “View T-SQL Query Generated By LINQ To SQL”
Trackbacks
- View T-SQL Query Generated By LINQ To SQL Using Linqpad | One .Net Way September 23rd, 2008
Leave a Reply
Get Updates By Email
Popular Post
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Free Icons And Images With Visual Studio 2008
- Windows 7 Control Panel In Classic Mode
- Dynamic Sort With LINQ
- Use SqlConnection With LINQ To SQL
- StyleCop Tutorial
- Write To Vista Event Log Using C#
- More Details Emerge On Microsoft Master Certification
Tag Cloud
Code Snippets
- Get Current Windows User In C#
- Get Width And Height Of Image In C#
- Get Windows Registry Size With WMI And C#
- Reverse Array Elements Using C#
- Convert Hexadecimal To Number In C#
- Get Free Disk Space Using T-SQL
- SQL Server 2008 – Get All Indexes In A Database
- Get Name Of Current Executing Assembly In C#
- Get CD Or DVD Drive Information Using WMI And C#
- Get Last Row From Table Using LINQ To SQL


September 23rd, 2008 at 7:14 am
just a though, wouldn’t it possible to have such a feature in linqpad?
http://www.linqpad.net/
September 23rd, 2008 at 9:37 am
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/
February 9th, 2010 at 10:37 pm
Deepak,
Tusi tum gr8 ho … good job .. keep it up.
March 4th, 2010 at 5:10 am
How I can see the T-SQL Genereted by Linq to SQL where using the CompiledQuery.compile