我在使用延续标记时遇到问题。
我有 16 个文档,想一次获取 5 个。
这就是对所提供方法的请求现在的工作方式: 1. 获取5,获取延续 token 。 工作正常。我收到了 5 份文件。
使用延续 token 可获得 5 个以上新的延续 token 。 工作正常。获取其他 5 份文件。
使用延续 token 再获得 5 个+新的延续 token 。 获取与步骤 1 相同的结果集 + 与步骤 1 相同的延续标记。
并且它始终在这两组和相同的两个 token 之间交替。
这是代码。
public async Task<Tuple<List<AdDocument>,string,int>> QueryWithPagingAsync(string continuationToken)
{
List<AdDocument> ads = new List<AdDocument>();
var totalAdsCount = await GetAdsCount();
if (string.IsNullOrEmpty(continuationToken))
{
FeedOptions options = new FeedOptions { MaxItemCount = 5 };
var query = Client.CreateDocumentQuery<AdDocument>(DocumentCollection.DocumentsLink, options).AsDocumentQuery();
var feedResponse = await query.ExecuteNextAsync<AdDocument>();
string continuation = feedResponse.ResponseContinuation;
foreach (var ad in feedResponse.AsEnumerable().OrderBy(a => a.Id))
{
ads.Add(ad);
}
return new Tuple<List<AdDocument>, string, int>(ads, continuation, totalAdsCount);
}
else
{
FeedOptions options = new FeedOptions { MaxItemCount = 5, RequestContinuation = continuationToken };
var query = Client.CreateDocumentQuery<AdDocument>(DocumentCollection.DocumentsLink, options).AsDocumentQuery();
var feedResponse = await query.ExecuteNextAsync<AdDocument>();
feedResponse = await query.ExecuteNextAsync<AdDocument>();
string continuation = feedResponse.ResponseContinuation;
foreach (var ad in feedResponse.AsEnumerable().OrderBy(a=>a.Id))
{
ads.Add(ad);
}
return new Tuple<List<AdDocument>, string, int>(ads, continuation, totalAdsCount);
}
}
最佳答案
所以我移动了“string continuation = feedResponse.ResponseContinuation;”到下面的执行,现在它可以工作了。
工作版本:
public async Task<Tuple<List<AdDocument>,string,int>> QueryWithPagingAsync(string continuationToken)
{
List<AdDocument> ads = new List<AdDocument>();
var totalAdsCount = await GetAdsCount();if (string.IsNullOrEmpty(continuationToken))
{
FeedOptions options = new FeedOptions { MaxItemCount = 5 };
var query = Client.CreateDocumentQuery<AdDocument>(DocumentCollection.DocumentsLink, options).AsDocumentQuery();
var feedResponse = await query.ExecuteNextAsync<AdDocument>();
foreach (var ad in feedResponse.AsEnumerable().OrderBy(a => a.Id))
{
ads.Add(ad);
}
string continuation = feedResponse.ResponseContinuation;
return new Tuple<List<AdDocument>, string, int>(ads, continuation, totalAdsCount);
}
else
{
FeedOptions options = new FeedOptions { MaxItemCount = 5, RequestContinuation = continuationToken };
var query = Client.CreateDocumentQuery<AdDocument>(DocumentCollection.DocumentsLink, options).AsDocumentQuery();
var feedResponse = await query.ExecuteNextAsync<AdDocument>();
foreach (var ad in feedResponse.AsEnumerable().OrderBy(a=>a.Id))
{
ads.Add(ad);
}
string continuation = feedResponse.ResponseContinuation;
return new Tuple<List<AdDocument>, string, int>(ads, continuation, totalAdsCount);
}
}
public async Task<Tuple<List<AdDocument>,string,int>> QueryWithPagingAsync(string continuationToken)
{
List<AdDocument> ads = new List<AdDocument>();
var totalAdsCount = await GetAdsCount();
if (string.IsNullOrEmpty(continuationToken))
{
FeedOptions options = new FeedOptions { MaxItemCount = 5 };
var query = Client.CreateDocumentQuery<AdDocument>(DocumentCollection.DocumentsLink, options).AsDocumentQuery();
var feedResponse = await query.ExecuteNextAsync<AdDocument>();
foreach (var ad in feedResponse.AsEnumerable().OrderBy(a => a.Id))
{
ads.Add(ad);
}
string continuation = feedResponse.ResponseContinuation;
return new Tuple<List<AdDocument>, string, int>(ads, continuation, totalAdsCount);
}
else
{
FeedOptions options = new FeedOptions { MaxItemCount = 5, RequestContinuation = continuationToken };
var query = Client.CreateDocumentQuery<AdDocument>(DocumentCollection.DocumentsLink, options).AsDocumentQuery();
var feedResponse = await query.ExecuteNextAsync<AdDocument>();
foreach (var ad in feedResponse.AsEnumerable().OrderBy(a=>a.Id))
{
ads.Add(ad);
}
string continuation = feedResponse.ResponseContinuation;
return new Tuple<List<AdDocument>, string, int>(ads, continuation, totalAdsCount);
}
}
关于c# - DocumentDB 分页标记。不断获得 "wrong"一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35183234/