tags:

views:

38

answers:

3

Is there any easy way to find out what T-SQL is being generated by my LINQ-to-SQL query?

Preferably using some LINQ entrypoint, rather than a licenced software product like Microsoft SQL Server Profiler or LINQ-to-SQL Profiler.

+1  A: 

If you have a Linq-to-SQL query in your C# code, just debug into your app and hover the mouse over the variable that takes the results - you should be able to inspect the actual SQL being used by that Linq-to-SQL query.

alt text

See fullsize image: http://tinypic.com/view.php?pic=okudf7&s=5

marc_s
You can see even more details with LINQ Query Visualizer: http://msdn.microsoft.com/en-us/library/bb629285.aspx
Giorgi
+1  A: 

You can use the Log property of DataContext class. Here is an example: DataContext.Log - Logging LINQ To SQL Output to Console or Debugger Output Window

Giorgi
+1  A: 

Call the Query's ToString() Method.

As long as the resultset is IQueryable<T>, the ToString() method will output the query:

dbDataContext db = new dbDataContext();

var query = from c in db.Customers
                    where c.Name == "foo"
                    select c;

Response.Write(query.ToString());
Atømix