我开始混淆ASP.NET MVC,并遇到了想要做一些基本Paging的问题。所以我用这个简单的LINQ语句(使用Northwind数据库),以获得10项特定页面:T-Sql中的“Over”子句究竟做了什么?
var q = (from p in db.Orders
orderby p.OrderDate descending
select p).Skip(currentPage * pageAmount).Take(pageAmount);
它运作良好,但后来当我调试,我很好奇,是由LINQ的产生什么实际的SQL到Sql。这是输出:
SELECT [t1].[OrderID], [t1].[CustomerID], [t1].[EmployeeID], [t1].[OrderDate], [t1].[RequiredDate], [t1].[ShippedDate], [t1].[ShipVia], [t1].[Freight], [t1].[ShipName], [t1].[ShipAddress], [t1].[ShipCity], [t1].[ShipRegion], [t1].[ShipPostalCode], [t1].[ShipCountry]
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY [t0].[OrderDate] DESC) AS [ROW_NUMBER], [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]
FROM [dbo].[Orders] AS [t0]
) AS [t1]
WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p0 + @p1
ORDER BY [t1].[ROW_NUMBER]
现在我不是SQL专家可言,这就是为什么我从来没有见过的“OVER”的条款。我只是从高层感到好奇,究竟是什么以及什么时候使用它?
谢谢!具有很多意义。 – BFree 2009-02-15 23:35:50