See SQL Generated by LINQ to Entities query

By Riley K

Code to see SQL Query generated by LINQ


For people new to LINQ sometimes we want see SQL Query generated by LINQ query

Suppose this is my simple query from entity context

//This is my Entity Context

  TestEmpModel1.TestEmpEntities1 tstContext = new TestEmpModel1.TestEmpEntities1();

I am fetcing Cou
ntry names from table

      
  var result = (from r in tstContext.Countries
                      select r).OrderBy(o => o.CountryName);

To see the SQL Generated simply cast it to ObjectQuery,

string str= (((System.Data.Objects.ObjectQuery)result).ToTraceString());

The output would be

SELECT
[Extent1].[ID] AS [ID],
[Extent1].[CountryName] AS [CountryName]
FROM [dbo].[Country] AS [Extent1]
ORDER BY [Extent1].[CountryName] ASC





See SQL Generated by LINQ to Entities query  (2177 Views)