Skip to content
Advertisement

How to convert this Entity Framework query to a SQL Server query

I have a query which is written in format like this:

How do I convert this query to a plain SQL Server query to see what its output is?

Advertisement

Answer

In Entity Framework there are a few ways to look at the SQL a query generates.

Note: All these ways will use this query:

  1. Cast IQueryable to an ObjectQuery and get it’s trace string:
  1. Intercept logging and print it to Debug.WriteLine:
  1. Entity Framework configuration.
    If you’re using EF 6.1 an onwards, you can use the EF configuration to create an interceptor and log it to a txt file as described here:
  1. And lastly, you could use LinqPad. There’s a free version that can connect to a database, and where you can directly enter C# expressions or statements and have the generated SQL show up at the bottom, similar to this Screenshot of LinqPad 6
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement