4

When I have a query generated like this:

var query = from x in Entities.SomeTable
            select x;

I can set a breakpoint and after hovering cursor over query I can see what will be the SQL command sent to database. Unfortunately I cannot do it when I use Count

var query = (from x in Entities.SomeTable
            select x).Count();

Of course I could see what comes to SqlServer using profiler but maybe someone has any idea how to do it (if it is possible) in VS.

vanpersil
  • 764
  • 1
  • 8
  • 26

3 Answers3

0

You can use ToTraceString():

ObjectQuery<SomeTable> query = (from x in Entities.SomeTable select x).Count();
Console.WriteLine(query.ToTraceString());
cSteusloff
  • 2,487
  • 7
  • 30
  • 51
0

You can use the Database.Log to log any query made like this :

using (var context = new MyContext()) 
{ 
    context.Database.Log = Console.Write; 

    // Your code here... 
}

Usually, in my context's constructor, I set that to my logger (whether it is NLog, Log4Net, or the stock .net loggers) and not the console, but actual logging tool is irrelevant.

For more information

Tipx
  • 7,367
  • 4
  • 37
  • 59
0

In EF6 and above, you can use the following before your query:

context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);

I've found this to be quicker than pulling up SQL Profiler and running a trace. Also, this post talks more about this topic: How do I view the SQL generated by the Entity Framework?