本文介绍了Cosmos DB阵列查询在.NET SDK中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取容器中使用的所有不同类别值,其中类别是字符串数组.

I'm attempting to get all distinct category values used in a container, where categories is an array of strings.

通过查看Cosmos DB SQL文档,我构建了以下查询,该查询在执行SQL并按预期返回不同类别的数组时可在仿真器中工作.

Looking at the Cosmos DB SQL docs, I've constructed the following query which works in the Emulator when I execute the SQL and returns an array of distinct categories as expected.

SELECT DISTINCT * FROM c IN s.categories

结果

[
    "Running",
    "Rugby",
    "Icon"
]

但是,如果我通过Cosmos .NET SDK使用相同的查询,则会收到错误消息

However, if I use the same query via the Cosmos .NET SDK I get an error

为什么该查询可在仿真器中而不是通过SDK进行?我是在做错什么还是SDK的问题?

Why would this query work in the Emulator but not via the SDK? Am I doing something wrong or this is an issue with the SDK?

样本数据:

{
"name": "Sample Entity",
"description": "<p></p>",
"startDateUtc": "2020-07-13T19:10:00Z",
"endDateUtc": "2020-07-13T20:00:00Z",
"categories": [
    "Running",
    "Icon"
],
"id": "h3foyhfk0a3betj1",
"dateCreated": "2020-07-15T06:33:57.9406583Z",
"lastUpdated": "2020-08-13T17:04:18.6182079Z" }

SDK代码:

var query = new QueryDefinition("SELECT DISTINCT * FROM c IN s.categories");
var results = new List<TEntity>();

var resultSetIterator = _container.GetItemQueryIterator<TEntity>(query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(partitionKey) });
while (resultSetIterator.HasMoreResults)
{
      var response = await resultSetIterator.ReadNextAsync();
      results.AddRange(response);
      if (response.Diagnostics != null)
      {
           _logger.LogTrace($"\nQueryWithSqlParameters Diagnostics: {response.Diagnostics}");
      }
}

return results;

推荐答案

删除,requestOptions:new QueryRequestOptions(){PartitionKey = new PartitionKey(partitionKey).

然后您将获得结果.使用时, requestOptions 中可能存在错误.

Then you will get result. There maybe wrong in requestOptions when you use.

var query = new QueryDefinition("SELECT DISTINCT * FROM c IN s.categories");
var results = new List<dynamic>();

var resultSetIterator = container.GetItemQueryIterator<dynamic>(query);
while (resultSetIterator.HasMoreResults)
{
    var response = await resultSetIterator.ReadNextAsync();
    results.AddRange(response);
    if (response.Diagnostics != null)
    {
        Console.WriteLine($"\nQueryWithSqlParameters Diagnostics: {response.Diagnostics}");
    }
}

这篇关于Cosmos DB阵列查询在.NET SDK中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:18