Visualize Generated SQL from Linq To Entities

I am looking for a way to find out what is the sql code that generated the L2E code for debugging purposes. I read Scott G. 's blogpost on the visualizer for Linq2SQL , but I can't get it to work for L2E.

Do you know how to visualize generated SQL from L2E?

I am using Visual Studio 2008 SP1 Professional.

+4
source share
2 answers

The ObjectQuery class has a ToTraceString () function. However, most of the queries you write in LINQ are created as IQueryable, so you must first classify them with ObjectQuery in order to use it.

or, if you define this extension method, you can use it with IQ

public static string ToTraceString<T>(this IQueryable<T> expression) { ObjectQuery<T> objectQuery = expression as ObjectQuery<T>; if (objectQuery != null) { return objectQuery.ToTraceString(); } return ""; } 

...

 //then you could use it like this IQueryable<Record> records = db.Record.Where(r=>r.Value > x); string generatedQuery = record.ToTraceString(); 
+7
source

Try the Sql server profile (if you have the Sql server installed). Open a new Trace window and there you will see all your queries issued against Sql Server.

+1
source

All Articles