views:

86

answers:

4

I an using VS2010, .NET4 and EF4. I would like to see the actual SQL that is generated when this is run. Also, what is this 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 };
+1  A: 

Run SQL Profiler

Mitch Wheat
SQL Profiler is a valid option, but maybe you should help him setup his filtering? So he doesn't have to sift through massive results...
Nix
Would the downvoter please leave a comment. Thanks.
Mitch Wheat
+3  A: 

You can Use SQL Server Profiler. LinqPad, and Im sure there are other tools

John Hartsock
LinqPad? How is he going to capture the SQL statement generated from EF using LinqPad?
Randy Minder
@Randy....LinqPad supports EF. you can see the Results of the query and the SQL it generated. See here http://www.linqpad.net/EntityFramework.aspx... see screen shots of the SQL statments here http://www.infoq.com/news/2009/02/linqpad
John Hartsock
+1 LinqPad works with EF(not during runtime), and its pretty easy to use as well. You can plug directly into DLLs, or Context, and they have a tab that lets you see SQL generated...
Nix
+9  A: 
var query = (from x in context.MyEntity where x... select x);

(query as ObjectQuery<MyEntity>).ToTraceString();

That will print to the trace log... if you want a simple trace viewer (outside of Visual Studio) check out DebugView

And as an additional side note, the best "real time" profiler I have used/seen out there is Entity Framework Profiler, you have to pay for it, but there is a trial version available, and it will give you SQL matched to the line of code. It also recognizes common "issues." Setup is stupid simple... all you have to add is one statement in the initializer of your application.


*Edit updated to show how to do it with users code *

I guess i can do all the hard work for you... ;). Since you use an annoynmous type just leave off the <T> part.

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());
Nix
I am new to this Entity Framework stuff. I have used SS 2.2 in the past. If there is a better/ more efficient way to write this, I would be grateful to learn it.
Scott and the Dev Team
are you selecting a subset of columns? aka a.Column1, a.Column2.. if you aren't excluding any I would just do select a;
Nix
I am using it in a foreach to build a table with the first row being the header and fieldGroupOrderLabel being the first column..
Scott and the Dev Team
I don't really understand without seeing. All I can suggest is to post a new question and outline what you are doing, basically show how you select and then how you use it and ask the community to validate it is the best/most efficient way..
Nix
I misunderstood "select a". I actually get it now. Thx for your help. I also noticed that this will work in the immediate window.((ObjectQuery) cklContactItems).ToTraceString()
Scott and the Dev Team
You are correct, if you set it to write to trace, it will also go to the output window during the debug sesison.
Nix
A: 

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

Serkan Hekimoglu