如何在Azure CosmosDB/DocumentDB中查询字典内容?

4

我正在使用C#代码将对象存储在Azure CosmosDB中。这些对象的属性在应用程序启动时未完全定义,因此可能会在运行时添加或删除一些属性。所以在模型类中有一个类型为Dictionary的属性"Attributes":

public Dictionary<string, string> Attributes { get; }

但是我该如何针对此属性编写查询呢?例如,我想编写以下查询:

但是我该如何针对此属性的内容编写查询呢?例如,我想编写以下查询:

documentQueryable
  .Where(doc => doc.Attributes.ContainsKey("City") && doc.Attributes["City"] == "NY");

然而,这不被支持:
Microsoft.Azure.Documents.Linq.DocumentQueryException: Method 'ContainsKey' is not supported., documentdb-dotnet-sdk/1.22.0 Host/32-bit MicrosoftWindowsNT/10.0.14393.0

你尝试过不用Contains吗?例如:documentQueryable.Where(doc => doc.Attributes["City"] == "NY"); - 没有“城市”键的文档不应该被返回。 - StuartLC
2个回答

4
由于Cosmos DB是无模式的,您不需要检查键是否存在。如果将代码更改为以下内容,则应按预期工作: documentQueryable.Where(doc => doc.Attributes["City"] == "NY");

字典键是否有不区分大小写的选项可用? - Matt Sanders

1
以下代码应该有效,你可以从这里阅读相关内容。
documentQueryable.Where(doc => doc.Attributes["City"] == "NY");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接