2015-06-04 90 views
2

可以說我有兩個集合,每個集合中有10個文檔。如果我想檢索最多15個文檔,推薦使用分頁的方法是什麼?DocumentDB分頁與許多集合

當我運行下面的代碼時,我得到20個結果。它開始用於請求returnes 10第一個集合,然後第二個也返回10,但它應該返回5,因爲MaxItemCount是15

 var batches = new List<IEnumerable<T>>(); 
     var feedOptions = new FeedOptions 
     { 
      MaxItemCount = 15 
     }; 

     var docQuery = Client.CreateDocumentQuery<T>(Database.SelfLink, feedOptions) 
       .Where(predicate).AsDocumentQuery(); 

     do 
     { 
      var batch = await docQuery.ExecuteNextAsync<T>(); 
      batches.Add(batch); 

     } while (docQuery.HasMoreResults); 

     var docs = batches.SelectMany(b => b).Take(maxItemCount.Value); 

     return docs; 

回答

3

MaxItemCount控制每頁結果數,而不是總返回結果。要減少結果總數,請更改while子句以檢查(例如,(docQuery.HasMoreResults & & batch.Length < = maxItemCount.Value)。

希望這會有所幫助。

+0

我已經試過這個結果相同。問題是數據何時位於多個集合中。在此示例中(MaxItemCount = 15),它從第一個集合中檢索了10個,它應該只從另一個集合中檢索5個,但它也檢索到10. –

+1

要做到這一點,您必須在客戶端管理集合迭代和分頁。即調用client.ResolveForRead()並枚舉集合自鏈接。然後在每個集合中,調用ExecuteNextAsync()並將MaxItemCount設置爲Math.Min(maxItemCount.value - batch.Length,15) –