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");
The following should work , you can read on the same from here
documentQueryable.Where(doc => doc.Attributes["City"] == "NY");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With