Abdus Salam Azad
Abdus Salam Azad

Reputation: 5502

LINQ query to sql query generator in visual studio 2010 or 2012

Is there any tools like watch in Visual Studio by which I can see the sql query that the LINQ query is generating during debug time using C#.

Upvotes: 1

Views: 1031

Answers (3)

Rex
Rex

Reputation: 2140

Apart from using the context.log, there are some other ways you can get the sql query.

  1. Use sql profiler, which is a free tool that can monitor all sql queries against a specific database server. by setting up a filter, you could get all the sql query against a specific database from a specific application. but bad thing is that it only show you executed/executing queries.

  2. linq to sql profilter, a tool from codesmith, could nicely help to visualize all queries, easy to track and debug, while also give good suggestions on query optimization.

Though for debugging purposes, context.log is good enough.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500675

If you use the Log property of the data context, that will write queries out as you execute them. For example:

using (var context = new FooDataContext())
{
    context.Log = Console.Out;
    // Execute a query here
}

I believe there is a way to visualize the SQL in the debugger interactively, but I can't remember it offhand. (It's entirely possible that Zaheer's suggestion of calling ToString will do it... or just inspecting the query in the debugger.)

Upvotes: 3

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28548

console the ToString() for query.

Example:

var query = from emp in Employees select emp;    
Console.WriteLine(query.ToString());

Upvotes: 1

Related Questions