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.
8 Responses to View T-SQL Query Generated By LINQ To SQL
Leave a Reply Cancel reply
Top Posts
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Windows 7 Control Panel In Classic Mode
- More Details Emerge On Microsoft Master Certification
- Use SqlConnection With LINQ To SQL
- Free Icons And Images With Visual Studio 2008
- Capture XML In WCF Service
- Dynamic Sort With LINQ
- StyleCop Tutorial
Tags
.Net 2010 ADO.NET ASP.NET Azure Blogging Books Browsers C# Certification Cloud Computing Code Snippets Community Data Services Eclipse Entity Framework Google IDE Java LINQ Mac Microsoft Museum NetBeans Office Oracle REST SharePoint Silverlight SQL Server T-SQL Tips Tools Training Visual Studio Visual Studio 2010 WCF Web Windows Windows 7 Windows Forms Windows Live WMI WPF XAML


just a though, wouldn’t it possible to have such a feature in linqpad?
http://www.linqpad.net/
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/
[...] my previous post I showed you three different ways to capture the T-SQL query generated by LINQ To SQL. A comment on [...]
Deepak,
Tusi tum gr8 ho … good job .. keep it up.
How I can see the T-SQL Genereted by Linq to SQL where using the CompiledQuery.compile
but when I do so only the results of the query will be shown
Tinsee,
You can use Linqpad to view the T-SQL query. Or have I not understood your question?
http://www.thereforesystems.com/view-t-sql-query-generated-by-linq-to-sql-using-linqpad/
Have you tried Linq to Entities (ObjectQuery) query visualizer (free) available at http://visualstudiogallery.msdn.microsoft.com/99468ece-689b-481c-868c-19e00e0a4e69
Regards
Venkat