You can use ToTraceString():

ObjectQuery<SomeTable> query = (from x in Entities.SomeTable select x).Count();


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


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?

Related Articles