Wriju's BLOG

.NET, Cloud and everything

LINQ to SQL: Finding T-SQL Query

LINQ to SQL (aka DLinq) generates SQL queries for us at the back. SqlMetal.exe does not contain any SQL query as it is pure object we do not prefer to keep anything which is not strongly typed. As you all know that LINQ to SQL creates the query for us. But the question is how it looks like? If you really want to verify the performance to make sure that LINQ to SQL is giving us the best query performance (always pain to DB developer)


 


Say, you have a small which retrieves the data from Northwind database. For more details please visit my entry on DLINQ: ADO.NET vNext a lap around


 


Northwind db = new Northwind(“northwindConnection”);


 


var query = from c in db.Customers


            where c.City == London


            select c;


 


foreach (var c in query)


    Console.WriteLine(c.CompanyName);


 


Very well it will return the values one by one. Then how is the query?


 


Yes we do have a property to get the SQL query inside the SqlMetal.exe generated class file. The name of the property is Log. Let’s see how we can implement it. I am using Console application. The code will look like


 


db.Log = Console.Out;


 


This will give you the SQL query in output window and the query would look like,


 


SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], …


FROM [Customers] AS [t0]


WHERE [t0].[City] = @p0


— @p0: Input NVarChar (Size = 6; Prec = 0; Scale = 0) NOT NULL [London]


SqlProvider\AttributedMetaModel


 


Hope this will help you to rectify more confusion.


 


Namoskar!!!