CSharp - Get SQL query via DataContext Log

Introduction

DataContext Log can output the equivalent SQL statement of an IQueryable<T> query prior to the parameter substitution.

You can then run the sql against the database to see the exact data coming back.


Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind");  
db.Log = Console.Out;  
IQueryable<Order> orders = from c in db.Customers  
                           from o in c.Orders  
                           where c.Country == "USA" && c.Region == "WA"  
                           select o;  
  
foreach(Order item in orders)  
  Console.WriteLine("{0} - {1} - {2}", item.OrderDate, item.OrderID, item.ShipName);  

Related Topic