How to find out the actual SQL that this statement generates?

I am using VS2010, .NET4 and EF4. I would like to see the actual SQL that is generated when it starts. Also, what is the best way to write this statement?

Here is my code:

var cklContactItems = from a in dbTestCenterViews.appvuChecklistExports where a.MarketChecklistID == MCLID && a.checklistSectionID == SID && a.fieldGroupOrder != null orderby a.fieldGroupOrder ascending select new { a.Column1, a.Column2, a.Column3, a.Column4, a.Column5,a.Column1FieldID,a.Column2FieldID,a.Column3FieldID,a.Column4FieldID,a.Column5FieldID,a.fieldGroupOrderLabel }; 
+4
source share
4 answers
 var query = (from x in context.MyEntity where x... select x); (query as ObjectQuery<MyEntity>).ToTraceString(); 

This will be printed in the trace log ... if you want a simple trace viewer (outside of Visual Studio) to exit DebugView

And as an additional note, the best โ€œreal-timeโ€ profiler I used / saw there, the Entity Framework Profiler , you have to pay for it, but there is a trial version and it will give you SQL corresponding to the line of code. He also acknowledges common "problems." Installation is silly simple ... all you have to add is one statement in the initializer of your application.


** Edit to show how to do this with user code **

I think I can do all the hard work for you ...;). Since you are using an unworthy type, just leave the <T> .

 var cklContactItems = from a in dbTestCenterViews.appvuChecklistExports where a.MarketChecklistID == MCLID && a.checklistSectionID == SID && a.fieldGroupOrder != null orderby a.fieldGroupOrder ascending select new { a.Column1, a.Column2, a.Column3, a.Column4, a.Column5, a.Column1FieldID, a.Column2FieldID, a.Column3FieldID, a.Column4FieldID, a.Column5FieldID, a.fieldGroupOrderLabel }; System.Diagnostics.Trace.WriteLine((query as ObjectQuery).ToTraceString()); 
+12
source

You can use SQL Server Profiler . LinqPad , and I'm sure there are other tools

+4
source
+1
source

You are using VS10, and there is a tool for this Intellitrace .

+1
source

All Articles