DocumentDB 分页标记。继续获得 "wrong" 个

DocumentDB pagination token. keep getting "wrong" one

我在使用延续标记时遇到问题。

我有 16 个文件,想一次取 5 个。

这是对所提供方法的请求现在的工作方式: 1. 获取5,获得continuation token。 工作正常。我得到了我的 5 个文件。

  1. 使用延续令牌获得 5 个以上的新延续令牌。 工作正常。获得 5 个其他文件。

  2. 使用延续令牌获得 5 个以上的+新延续令牌。 获得与步骤 1 相同的结果集 + 与步骤 1 相同的延续标记。

并且它一直在这两组和相同的两个标记之间交替。

这是代码。

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);
        }

    }