【发布时间】:2020-09-25 22:14:53
【问题描述】:
我正在尝试检索与 Cosmos DB 中的分区键匹配的记录。我正在使用以下代码:
//sec.id is Guid, s.key is the partition key (also a Guid)
var query = sectionsContainer.GetItemLinqQueryable<Post>().Where(s => s.type == "post" && s.key == sec.id);
var qdef = query.ToQueryDefinition();
var iterator = postsContainer.GetItemQueryIterator<Post>(qdef);
List <Post> posts = new List<Post>();
while (iterator.HasMoreResults)
{
var response = await iterator.ReadNextAsync();
posts.AddRange(response.ToList());
}
return posts;
Cosmos DB 不返回任何内容。 Linq 生成的 SQL 查询如下:
SELECT VALUE root FROM root WHERE ((root["type"] = "post") AND (root["key"] = "9bb4fcdc-2077-4a0b-9041-81752f06d011"))
当复制并粘贴到 Azure 门户的数据资源管理器时,查询会返回正确的结果:
[
{
"id": "52d2abbd-d777-4c3c-b02c-a940eb49fdc4",
"type": "post",
"key": "9bb4fcdc-2077-4a0b-9041-81752f06d011", //Doesn't work when querying against this
...
"section": {
"id": "9bb4fcdc-2077-4a0b-9041-81752f06d011", //Works when querying against this
"type": "section",
...
}
//Data removed for brevity
},
// Second & third item...
]
当我修改查询以匹配 [document].section.id == sec.id 时,Linq / Cosmos DB 返回正确的数据。
这里的问题是为什么查询在使用分区键时不起作用但在使用任何其他属性时起作用?
【问题讨论】:
-
我认为我们需要更多信息:你确定这是生成的 sql 吗?你是怎么捕捉到的?你能显示
sec.id的确切值吗? -
您可以通过检查 qdef 变量在调试模式下检查生成的 SQL,我确信该 SQL 是正确的。 sec.id 变量值为“9bb4fcdc-2077-4a0b-9041-81752f06d011”,也在调试器和生成的 SQL 中确认。
-
好的,谢谢,我只是仔细检查了一下,因为我习惯了更多类似参数的链接查询。
-
&%!#?%!!当然,问题(再一次)在椅子和键盘之间。查询是针对“section”容器生成的,但针对“posts”容器运行。只用了 6 个小时的头撞墙。
-
.... 抱歉,但是:大声笑...这发生在我们所有人身上 ;-)
标签: c# linq .net-core azure-cosmosdb azure-cosmosdb-sqlapi