简体   繁体   中英

How to query against Dictionary contents in Azure CosmosDB/DocumentDB?

I am storing objects in Azure CosmosDB from C# code. The properties of these objects are not fully defined when the application starts, so some may be added or removed at runtime. That is why I have a property "Attributes" of type Dictionary in the model class:

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

But how do I write queries against the contents of this property? For example, I would like to write a query like:

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

However, this is not supported:

Microsoft.Azure.Documents.Linq.DocumentQueryException: Method 'ContainsKey' is not supported., documentdb-dotnet-sdk/1.22.0 Host/32-bit MicrosoftWindowsNT/10.0.14393.0

Because Cosmos DB is schemaless you don't need to check the key exists. If you change your code to the following it should work as expected:

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

以下应该工作,您可以从here阅读

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM