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.
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.
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.
See fullsize image: http://tinypic.com/view.php?pic=okudf7&s=5
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
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());