Thursday, August 2, 2012

How to view the SQL generated by LINQ to Entities?

There is a ToTraceString() method for objectQuery. The anonymous object returned from the LINQ query has to be casted into ObjectQuery as shown below to use the ToTraceString():

var provQuery = from a in entities.Table1 where a.ID == id orderby a.Name
                            select a;

The SQL generated from the LINQ can be viewed by using as below:

((System.Data.Objects.ObjectQuery)provQuery).ToTraceString()

You can copy from watch window and run the sql query to test it.

No comments:

Post a Comment