gpt4 book ai didi

azure-cosmosdb - Cosmos DB 数组查询在 .NET SDK 中不起作用

转载 作者:行者123 更新时间:2023-12-04 09:08:26 25 4
gpt4 key购买 nike

我正在尝试获取容器中使用的所有不同类别值,其中类别是一个字符串数组。
查看 Cosmos DB SQL 文档,我构建了以下查询,当我执行 SQL 并按预期返回不同类别的数组时,该查询在模拟器中工作。

SELECT DISTINCT * FROM c IN s.categories
结果是
[
"Running",
"Rugby",
"Icon"
]
但是,如果我通过 Cosmos .NET SDK 使用相同的查询,则会出现错误

"Identifier 'session' could not be resolved."


为什么这个查询可以在模拟器中运行而不是通过 SDK 运行?我做错了什么还是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) .
enter image description here
然后你会得到结果。 requestOptions 中可能有错误当你使用。

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

关于azure-cosmosdb - Cosmos DB 数组查询在 .NET SDK 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63402515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com