Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to see generated sql from a linq query

Tags:

sql

linq

Just trying to get the sql that is generated by a linq query.

like image 510
RayLoveless Avatar asked May 31 '11 16:05

RayLoveless


People also ask

How do I view SQL generated by Entity Framework?

To view the SQL that will be generated, simply call ToTraceString() . You can add it into your watch window and set a breakpoint to see what the query would be at any given point for any LINQ query. You can attach a tracer to your SQL server of choice, which will show you the final query in all its gory detail.

How can I see the LINQ query in SQL Profiler?

Now, let's run the Application and run the trace in SQL Server Profiler. Click on Get Data button, the data will be populated on the page. Go to SQL Server Profiler and stop the trace, where you will see T-SQL statement of our stored procedure, as shown below. Notice that GetEmployees stored procedure is executed.


2 Answers

With Linq2Sql

dc.GetCommand(query).CommandText 

see http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.getcommand.aspx for more info.

But I usually use LinqPad

like image 194
sgmoore Avatar answered Oct 07 '22 05:10

sgmoore


There are 3 ways do that.

1.You can use LINQPad.It's Free http://www.linqpad.net/

2.You can use SQL Server Profiler inside the Sql Server (Tools --> SQL Server Profiler)

3.You can use Visual Studio Debugger for Generate T-Sql.(with any visual studio version)

like image 28
Sampath Avatar answered Oct 07 '22 06:10

Sampath