Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to see the SQL query of a LINQ command?

Tags:

.net

sql

linq

It would help me understand LINQ a little bit better that's all.

I tried using the SQL Profiler but I'm not sure how I can get it to show the actual SQL commands.

like image 318
Diskdrive Avatar asked Mar 08 '11 00:03

Diskdrive


2 Answers

ScottGu's blog has a great post on the subject here: http://weblogs.asp.net/scottgu/archive/2006/09/01/Understanding-LINQ-to-SQL-Query-Translations.aspx

There is a query visualizer you can use by hovering over the object in the debugger.

like image 129
Matthew Avatar answered Nov 02 '22 03:11

Matthew


Use the Log property of the DataContext object:

using (var dc = new DataContext())
{
    dc.Log = Console.Out;  // Outputs the SQL statement to a System.IO.TextWriter object

    var customers = dc.Customers.Single(c => c.Customer_ID == 7);
}
like image 29
Neil T. Avatar answered Nov 02 '22 04:11

Neil T.