DocumentDB分页标记。不断得到错误"一

时间:2016-02-03 17:01:42

标签: c# azure azure-cosmosdb

我遇到了延续令牌的问题。

我有16个文件,并希望一次获取5个。

这是对提供的方法的请求现在的工作方式: 1.获取5,获取延续令牌。 工作良好。我得到了我的5份文件。

  1. 使用延续令牌获得5个以上+新的延续令牌。 工作良好。获取5个其他文档。

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

  3. 它始终在这两组和两个相同的标记之间保持交替。

    这是代码。

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

1 个答案:

答案 0 :(得分:0)

所以我移动了&#34;字符串continuation = feedResponse.ResponseContinuation;&#34;在执行之下,现在它可以工作。

工作版:

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

    }