获取EF查询的SQL语句

在EF编程中我们能够通过lamda表达式能够进行查询数据。获取到IQueryable<T>结果,我们要想知道详细的SQL语句是什么须要使用ObjectQuery<T>进行处理

处理方法例如以下:

            IQueryable<Data> data = this.ObjectContext.Data.Where(d => d.Name.StartsWith("測试"));
            ObjectQuery<Data> oq=data as ObjectQuery<Data>;
            String sql=oq.ToTraceString();

执行结果例如以下:

SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[Name] AS [Name]
FROM (SELECT 
      [Data].[ID] AS [ID],    
      [Data].[Name] AS [Name]
      FROM [dbo].[Data] AS [Data]) AS [Extent1]
WHERE [Extent1].[Name] LIKE '測试%'


posted @ 2017-07-21 16:04  lytwajue  阅读(1161)  评论(0编辑  收藏  举报