2011-03-17 44 views

回答

14

如果執行此LINQ查詢:

var data = context.Posts.OrderBy(p => p.Id).Skip(20).Take(20).ToList(); 

它會產生這樣的SQL:

SELECT TOP (20) 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Text] AS [Text] 
FROM (SELECT 
     [Extent1].[Id] AS [Id], 
     [Extent1].[Text] AS [Text], 
     row_number() OVER (ORDER BY [Extent1].[Id] ASC) AS [row_number] 
    FROM [dbo].[Posts] AS [Extent1] 
) AS [Extent1] 
WHERE [Extent1].[row_number] > 20 
ORDER BY [Extent1].[Id] ASC 

它不是這樣的漂亮,就像您在您的存儲過程,但在意義編寫自定義的SQL的表現是一樣的。分頁是在數據庫上完成的。

+1

要確認,你要說上面的LINQ查詢將產生一個結果,其性能與我們上面使用SQL查詢完全一樣......是嗎? – 2011-03-19 05:27:34

+1

是的。上面的linq查詢是作爲SQL執行的。 – 2011-03-19 09:33:02