Peter
Peter

Reputation: 14508

Visualize generated SQL from Linq To Entities

I am looking for a way to see what the sql is that my L2E code has generated for debugging purposes. I have read a blogpost by Scott G. on a visualizer for Linq2SQL but I can't get it to work for L2E.

Do you know of some way to visualize the generated SQL from L2E?

I am using Visual Studio 2008 SP1 Professional.

Upvotes: 4

Views: 1802

Answers (2)

emipasat
emipasat

Reputation:

Try Sql Server Profile (if you have Sql Server installed). Open a new Trace window and there you'll see all your queries issued against Sql Server.

Upvotes: 1

Tion
Tion

Reputation: 1490

The Class ObjectQuery has a ToTraceString() function. However, most queries that you write in LINQ are created as IQueryable so you first have to cast them to an 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();

Upvotes: 6

Related Questions