2009-10-20 67 views
2

我在實體框架中使用.Skip和.Take方法。 .Skip調用在生成SQL時正在使用。這個.Take不是。此代碼:爲什麼實體框架在生成SQL時忽略Take()方法?

 public IList<DocPullRun> GetDocRunsPaginated(int startRowIndex, int maximumRows) { 

     Logger.Debug("GetDocRunsPaginated: startRowIndex: {0}, maximumRows: {1}", startRowIndex, maximumRows); 
     Debug.Assert(startRowIndex >= 0); 
     IOrderedQueryable<DocPullRun> sortedPulls = 
      from run in DB.DocPullRuns 
       .Include("DocumentPullDefinition") 
       .Include("DocumentPullDefinition.Case") 
       .Include("DocumentPullDefinition.DocCategory") 
       .Include("DocumentPullDefinition.Repository") 
       .Include("DocumentPullDefinition.Repository.ConcordanceRepository") 

      orderby run.PullStarted descending 
      select run; 

     IQueryable<DocPullRun> query = sortedPulls.Skip(startRowIndex); 
     if (maximumRows > 0) 
      query.Take(maximumRows); 

     return query.ToList(); 
    } 

結果列在下面所示的本SQL(注意,在這種呼叫,startRowIndex是0和maximumRows是)。 Where子句包含WHERE [Project1]。[row_number]> 0(即.Skip部分),但我期望看到一個選擇TOP 10,但是隻是看到Select。結果列表中有超過10個元素。請注意,我運行SQL跟蹤並查看SQL確實被延遲,直到最後一行才發送:query.ToList()。這是一些類型的錯誤?

SELECT 
[Project1].[C1] AS [C1], 
[Project1].[Id] AS [Id], 
[Project1].[PullStarted] AS [PullStarted], 
[Project1].[PullEnded] AS [PullEnded], 
[Project1].[MatchedQuery] AS [MatchedQuery], 
[Project1].[NewDocs] AS [NewDocs], 
[Project1].[UpdatedDocs] AS [UpdatedDocs], 
[Project1].[DeletedDocs] AS [DeletedDocs], 
[Project1].[Errors] AS [Errors], 
[Project1].[C2] AS [C2], 
[Project1].[Id1] AS [Id1], 
[Project1].[Requestor] AS [Requestor], 
[Project1].[RequestTime] AS [RequestTime], 
[Project1].[Query] AS [Query], 
[Project1].[Enabled] AS [Enabled], 
[Project1].[LastPullTime] AS [LastPullTime], 
[Project1].[Case_ID] AS [Case_ID], 
[Project1].[Case_Name] AS [Case_Name], 
[Project1].[Client_ID] AS [Client_ID], 
[Project1].[Matter_ID] AS [Matter_ID], 
[Project1].[Is_Active] AS [Is_Active], 
[Project1].[Requires_Case_Specific_Permissions] AS [Requires_Case_Specific_Permissions], 
[Project1].[Last_Update] AS [Last_Update], 
[Project1].[C3] AS [C3], 
[Project1].[Category_ID] AS [Category_ID], 
[Project1].[CategoryName] AS [CategoryName], 
[Project1].[DefaultGridLayout] AS [DefaultGridLayout], 
[Project1].[C4] AS [C4], 
[Project1].[Repository_ID] AS [Repository_ID], 
[Project1].[Description] AS [Description], 
[Project1].[OfficeAbbr] AS [OfficeAbbr], 
[Project1].[C5] AS [C5], 
[Project1].[Repository_ID1] AS [Repository_ID1], 
[Project1].[ConcordanceDBId] AS [ConcordanceDBId], 
[Project1].[Repository_ID2] AS [Repository_ID2], 
[Project1].[RepositoryType_Id] AS [RepositoryType_Id] 
FROM (SELECT [Project1].[Id] AS [Id], [Project1].[PullStarted] AS [PullStarted], [Project1].[PullEnded] AS [PullEnded], [Project1].[MatchedQuery] AS [MatchedQuery], [Project1].[NewDocs] AS [NewDocs], [Project1].[UpdatedDocs] AS [UpdatedDocs], [Project1].[DeletedDocs] AS [DeletedDocs], [Project1].[Errors] AS [Errors], [Project1].[Id1] AS [Id1], [Project1].[Requestor] AS [Requestor], [Project1].[RequestTime] AS [RequestTime], [Project1].[Query] AS [Query], [Project1].[Enabled] AS [Enabled], [Project1].[LastPullTime] AS [LastPullTime], [Project1].[Case_ID] AS [Case_ID], [Project1].[Case_Name] AS [Case_Name], [Project1].[Client_ID] AS [Client_ID], [Project1].[Matter_ID] AS [Matter_ID], [Project1].[Is_Active] AS [Is_Active], [Project1].[Requires_Case_Specific_Permissions] AS [Requires_Case_Specific_Permissions], [Project1].[Last_Update] AS [Last_Update], [Project1].[Category_ID] AS [Category_ID], [Project1].[CategoryName] AS [CategoryName], [Project1].[DefaultGridLayout] AS [DefaultGridLayout], [Project1].[Repository_ID] AS [Repository_ID], [Project1].[Description] AS [Description], [Project1].[OfficeAbbr] AS [OfficeAbbr], [Project1].[Repository_ID1] AS [Repository_ID1], [Project1].[ConcordanceDBId] AS [ConcordanceDBId], [Project1].[Repository_ID2] AS [Repository_ID2], [Project1].[RepositoryType_Id] AS [RepositoryType_Id], [Project1].[C1] AS [C1], [Project1].[C2] AS [C2], [Project1].[C3] AS [C3], [Project1].[C4] AS [C4], [Project1].[C5] AS [C5], row_number() OVER (ORDER BY [Project1].[PullStarted] DESC) AS [row_number] 
    FROM (SELECT 
     [Extent1].[Id] AS [Id], 
     [Extent1].[PullStarted] AS [PullStarted], 
     [Extent1].[PullEnded] AS [PullEnded], 
     [Extent1].[MatchedQuery] AS [MatchedQuery], 
     [Extent1].[NewDocs] AS [NewDocs], 
     [Extent1].[UpdatedDocs] AS [UpdatedDocs], 
     [Extent1].[DeletedDocs] AS [DeletedDocs], 
     [Extent1].[Errors] AS [Errors], 
     [Extent2].[Id] AS [Id1], 
     [Extent2].[Requestor] AS [Requestor], 
     [Extent2].[RequestTime] AS [RequestTime], 
     [Extent2].[Query] AS [Query], 
     [Extent2].[Enabled] AS [Enabled], 
     [Extent2].[LastPullTime] AS [LastPullTime], 
     [Extent3].[Case_ID] AS [Case_ID], 
     [Extent3].[Case_Name] AS [Case_Name], 
     [Extent3].[Client_ID] AS [Client_ID], 
     [Extent3].[Matter_ID] AS [Matter_ID], 
     [Extent3].[Is_Active] AS [Is_Active], 
     [Extent3].[Requires_Case_Specific_Permissions] AS [Requires_Case_Specific_Permissions], 
     [Extent3].[Last_Update] AS [Last_Update], 
     [Extent4].[Category_ID] AS [Category_ID], 
     [Extent4].[CategoryName] AS [CategoryName], 
     [Extent5].[DefaultGridLayout] AS [DefaultGridLayout], 
     [Extent6].[Repository_ID] AS [Repository_ID], 
     [Extent6].[Description] AS [Description], 
     [Extent6].[OfficeAbbr] AS [OfficeAbbr], 
     [Extent9].[Repository_ID] AS [Repository_ID1], 
     [Extent9].[ConcordanceDBId] AS [ConcordanceDBId], 
     [Extent12].[Repository_ID] AS [Repository_ID2], 
     [Extent13].[RepositoryType_Id] AS [RepositoryType_Id], 
     1 AS [C1], 
     1 AS [C2], 
     1 AS [C3], 
     1 AS [C4], 
     1 AS [C5] 
     FROM   [dbo].[DocPullRuns] AS [Extent1] 
     LEFT OUTER JOIN [dbo].[DocumentPullDefinitions] AS [Extent2] ON [Extent1].[DocumentPullDefinitionId] = [Extent2].[Id] 
     LEFT OUTER JOIN [dbo].[Cases] AS [Extent3] ON [Extent2].[CaseId] = [Extent3].[Case_ID] 
     LEFT OUTER JOIN [dbo].[DocCategories] AS [Extent4] ON [Extent2].[CategoryId] = [Extent4].[Category_ID] 
     LEFT OUTER JOIN [dbo].[DocCategories] AS [Extent5] ON [Extent2].[CategoryId] = [Extent5].[Category_ID] 
     LEFT OUTER JOIN [dbo].[Repositories] AS [Extent6] ON [Extent2].[RepositoryId] = [Extent6].[Repository_ID] 
     LEFT OUTER JOIN (SELECT [Extent7].[Repository_ID] AS [Repository_ID3], [Extent7].[RepositoryType_Id] AS [RepositoryType_Id], [Extent7].[Description] AS [Description], [Extent7].[OfficeAbbr] AS [OfficeAbbr], [Extent8].[Repository_ID] AS [Repository_ID4], [Extent8].[ConcordanceDBId] AS [ConcordanceDBId] 
      FROM [dbo].[Repositories] AS [Extent7] 
      LEFT OUTER JOIN [dbo].[ConcordanceRepositories] AS [Extent8] ON [Extent7].[Repository_ID] = [Extent8].[Repository_ID]) AS [Join6] ON [Extent2].[RepositoryId] = [Join6].[Repository_ID3] 
     LEFT OUTER JOIN [dbo].[ConcordanceRepositories] AS [Extent9] ON [Join6].[Repository_ID4] = [Extent9].[Repository_ID] 
     LEFT OUTER JOIN (SELECT [Extent10].[Repository_ID] AS [Repository_ID5], [Extent10].[RepositoryType_Id] AS [RepositoryType_Id], [Extent10].[Description] AS [Description], [Extent10].[OfficeAbbr] AS [OfficeAbbr], [Extent11].[Repository_ID] AS [Repository_ID6], [Extent11].[ConcordanceDBId] AS [ConcordanceDBId] 
      FROM [dbo].[Repositories] AS [Extent10] 
      LEFT OUTER JOIN [dbo].[ConcordanceRepositories] AS [Extent11] ON [Extent10].[Repository_ID] = [Extent11].[Repository_ID]) AS [Join9] ON [Extent2].[RepositoryId] = [Join9].[Repository_ID5] 
     LEFT OUTER JOIN [dbo].[ConcordanceRepositories] AS [Extent12] ON [Join9].[Repository_ID6] = [Extent12].[Repository_ID] 
     LEFT OUTER JOIN [dbo].[Repositories] AS [Extent13] ON [Extent2].[RepositoryId] = [Extent13].[Repository_ID] 
    ) AS [Project1] 
) AS [Project1] 
WHERE [Project1].[row_number] > 0 
ORDER BY [Project1].[PullStarted] DESC 

回答

7

query.Take()不改變查詢;而是返回代碼忽略的新的表達式。因此,將您的代碼更改爲:

if (maximumRows > 0) 
    query = query.Take(maximumRows); 
+0

+1:在採取呼叫尼斯漁獲物。 – 2009-10-20 16:24:06

+0

Arghh!菜鳥錯誤!謝謝克雷格! – 2009-10-20 16:33:52

-1

如果您只想要最大行數,則不需要if語句。如果它沒有達到最大值,那麼它只會返回所得到的值。

你可以只說:

return query.Take(maximumRows).ToList(); 
+0

這隻會在'maximumRows'高於0時起作用。如果'maximumRows'爲0,則不會顯示任何行。但在他的例子中,他想要返回數據庫中的所有行。 – TryingToImprove 2013-05-27 12:31:32