3

How would i know the SQL statement generated by my Linq to sql query?

3 Answers 3

6

You could see the SQL statement by using the toString() statement.

var customers = from cust in Customers
        select cust;

Console.WriteLine(customers.ToString());

or you could do something like this.

DataContext context = new DataContext(...);
StringWriter writer = new StringWriter();
context.Log = writer;

var customers = from cust in Customers
        select cust;

Console.WriteLine(writer.ToString());
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, who knew. I've been using LINQ for a couple of years now and never had any idea that Query.ToString would return the SQL command.
3

Use LINQ to SQL Debugger Visualizer.

Alternatively, you can set dataContext.Log property to Console.Out or something and the SQL statement, along with actual parameter values will be written out to that stream.

Comments

0

There is a tool to check the query http://www.linqpad.net/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.