Get generated sql from compiled linq query

Is it possible to get the generated SQL from a compiled linq query?

+7
source share
3 answers

You can:

  • Use the context log property to redirect the generated request to the Visual Studio output window. link
  • Or use LINQ to SQL Debug Visualizer. link
+9
source

Use LinqPad :

Or, alternatively, you can use the sql server profiler to view the query. I know that earlier you could use the request variable in debugging, and it will show you the request that it is going to execute, but I'm not quite sure if this still works (definitely not on client applications)

+2
source

Thanks jfs, but the link in your option # 1 is no longer good. There is no corresponding article in it. Chris B's link to an MSDN article helped me.

Here is my solution, as my application is not console:

TextWriter tw = new StringWriter(); db.Log = tw; IQueryable<Customer> custQuery = from cust in db.Customers where cust.City == "London" select cust; string output = tw.ToString(); // output variable has the generate SQL now 
0
source

All Articles