我開始混淆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