views:

295

answers:

3

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.

+2  A: 

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();
Tion
A: 

Hi, Have you tried L2E debugger visualizer at http://www.rajavenkatesh.com/projects.aspx ?

A: 

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.