Just trying to get the sql that is generated by a linq query.
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.
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.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With