c# - DocumentDB 分页标记。不断获得 "wrong"一个

标签 c# azure azure-cosmosdb

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

我有 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/

    相关文章:

    azure - Azure Active Directory 是否具有 OAuth/OpenID Connect token 自省(introspection)端点?

    azure-cosmosdb - 如何编写 Azure Cosmos COUNT DISTINCT 查询

    Azure Powershell - 从另一个脚本文件加载变量

    azure - 无法在 VSTS 中构建编译的 Azure 函数(错误 MSB4019)

    azure-cosmosdb - 无服务器帐户不支持在容器上设置报价吞吐量或自动驾驶仪。 Azure Cosmos DB 迁移工具

    azure-cosmosdb - 使用 Gremlin 进行地理位置搜索

    c# - 按连接 Winforms 的用户插入记录

    c# - 如何从时间服务器设置日期/时间

    c# - 这是如何抛出 InvalidCastException

    c# - JsonSerializerSettings 线程安全吗?