Is there a way to get the SQL created using the LINQ query?

I am developing an ASP.NET page and am using LINQ to work with MS SQL Server. I am fine with basic SQL, but I am much better off developing queries using LINQ. I know they are similar, but it’s easier for me to develop complex queries in LINQ. My question is this: is there a way to design a query in LINQ and then get the SQL that it generated? I would like to embed SQL in a stored procedure, since several pages (out of my control) will have to make the same query.

+5
source share
4 answers

Yes. The LINQ database context has the Log property , which displays the SQL code that it executed. You can also do this through the free LinqPad product and the commercial Linqer product.

+6
source

You can get it in two ways:

  • Use LINQPad
  • use ToString () in the query to get its SQL form:

    var query = from x to SomeTable where x.SomeField == 5 select x.SomeOtherField; pe (query.ToString ());

+3
source
+1

linq, SQL Server, SQL Server Profiler. , SQL, .

/SQL Server Profiler .

Fulfill your query in your application, grab the exit from Profiler Insert into SQL Server Query Analyzer

+1
source

All Articles