在代码中,获取Entity Framework生成的T-SQL查询语句

 
AdventureWorksLT2008Entities db = new AdventureWorksLT2008Entities();
 
 var querys = db.Products.Where(p => p.ProductID % 55 == 0).Select(p => new { Id = p.ProductID, LPrice = p.ListPrice, Name = p.Name });

 foreach (var item in querys)
 {
     Console.WriteLine(item.Id+". "+item.LPrice+" "+item.Name);
 }

 Console.WriteLine(((System.Data.Objects.ObjectQuery)querys).ToTraceString());

// 715. 49.9900 Long-Sleeve Logo Jersey, L
// 770. 782.9900 Road-650 Black, 52
// 825. 327.2150 HL Mountain Rear Wheel
// 880. 54.9900 Hydration Pack - 70 oz.
// 935. 40.4900 LL Mountain Pedal
// 990. 539.9900 Mountain-500 Black, 42
// SELECT
// [Extent1].[ProductID] AS [ProductID],
// [Extent1].[ListPrice] AS [ListPrice],
// [Extent1].[Name] AS [Name] 
// FROM [SalesLT].[Product] AS [Extent1]
// WHERE 0 = ([Extent1].[ProductID] % 55)
posted @ 2012-03-12 21:46  joe62  阅读(2388)  评论(2编辑  收藏  举报