RayLoveless
RayLoveless

Reputation: 21108

how to see generated sql from a linq query

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

Upvotes: 32

Views: 98329

Answers (7)

Sampath
Sampath

Reputation: 65988

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)

Upvotes: 21

KmKrishna
KmKrishna

Reputation: 1

You can use Diagnostic tools in Visual Studio to see the generated query.

To enable this, go to top right corner, there you can see quick search and type diagnostic tools.

In the results, click on appropriate option and see below for query generated.

Upvotes: 0

Mudasir Younas
Mudasir Younas

Reputation: 872

The easiest way i could suggest is to go with Database Log. Put Logafter initializing DataContext and you will be able to track whatever is done by EF on visual studio output window.

  DataContext db = new DataContext();
  db.Database.Log = generatedSQL =>
   {
      Debug.WriteLine(generatedSQL);
   };

Upvotes: 1

Conrad Frix
Conrad Frix

Reputation: 52675

Another way

From the MSDN article How to: Display Generated SQL (LINQ to SQL)

Set the DataContext.Log Property to Console.Out and you'll see it in the console

Upvotes: 3

sgmoore
sgmoore

Reputation: 16077

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

Upvotes: 33

Neil Knight
Neil Knight

Reputation: 48587

Use SQL Profiler if you are using SQL Server as your database.

Upvotes: 7

Related Questions