Hello,
I have quite unique situation to handle (I guess). So the scenario is that I have a CosmosDb and a model of data looking like this:
{
"deviceId": "exampleId",
"properties": [
{
"name": "temperature",
"value": 20
},
{
"name": "humidity",
"value": 10
}
]
}
while the C# class looks like this:
public class
{
public string DeviceId { get; set; }
public IEnumerable<PropertyObject> Properties { get; set; }
}
public class PropertyObject
{
public string Name { get; set; }
public object Value { get; set; }
}
So the problem is that I need this to be filtered by PropertyObject and its Value.
There wouldn't be any issues if this was a hard-typed model but in this case Value is an object and can be a lot of things. Sometimes it might be a string, a decimal or an integer.
What I have tried:
And obviously this doesn't work:
properties.Where( x => x.Name == "temperature" && x.Value > 20);
Since Value is an object. It'll work if I add the casting:
properties.Where( x => x.Name == "temperature" && (int)x.Value > 20);
But I can't hardcode it since every propertyobject might have different type of value in Value property (int,decimal,datetime,string etc.)
Do you have any ideas how can this be handled for filtering CosmosDb in C# using latest Azure Cosmos SDK?
I have a free hand to change the C# code and I can access the information about the type of a particular property (from config file).
Maybe the Expression Builder for LINQ would be a good way here?
I'm also thinking about how would this work if the model was a dynamic type instead, maybe this would allow something. I don't know, I'm not that proficient at working with nosql db like Cosmos.