Viewing the generated SQL statements

You might wonder which SQL statements are used by LINQ to Entities to interact with the databases. In this section, we will use two ways to view the generated SQL statements used by LINQ to Entities queries. The first one is to use the ToString method and the second one is to use SQL Profiler.

Viewing the SQL statements using ToString

First, let's write a new test method to contain one LINQ to Entities query:

        static void ViewGeneratedSQL()
        {
            using(var NWEntities = 
                new NorthwindEntities())
           {

            var beverages = 
                from p in NWEntities.Products
                where p.Category.CategoryName == "Beverages"
                orderby p.ProductName
                select p;
            }
        }

Now, we can print out the SQL statement of the LINQ to Entities query using the following statement: ...

Get WCF Multi-layer Services Development with Entity Framework Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.