我正在阅读一篇关于Linq to Sql的文章并遇到了这个问题:
IQueryProvider provider = new QueryProvider(database.GetCommand, database.ExecuteQuery);
IQueryable<Product> source = new Queryable<Product>(provider, database.GetTable<Product>());
IQueryable<string> results = source.Where(product => product.CategoryID == 2)
.OrderBy(product => product.ProductName)
.Select(product => product.ProductName)
.Skip(5)
.Take(10);
然后作者将结果翻译成纯 sql:
exec sp_executesql N'SELECT [t1].[ProductName]
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY [t0].[ProductName]) AS [ROW_NUMBER], [t0].[ProductName]
FROM [dbo].[Products] AS [t0]
WHERE [t0].[CategoryID] > @p0
) AS [t1]
WHERE [t1].[ROW_NUMBER] BETWEEN @p1 + 1 AND @p1 + @p2
ORDER BY [t1].[ROW_NUMBER]',N'@p0 int,@p1 int,@p2 int',@p0=2,@p1=5,@p2=10
我心想,“天啊!如果有一个 IQueryable 的扩展可以在调试时为你生成这些字符串,那不是很好吗?”
任何人都听说过这样的事情,如果是这样,你能指出我正确的方向吗?
谢谢!