Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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:

C#
properties.Where( x => x.Name == "temperature" && x.Value > 20);

Since Value is an object. It'll work if I add the casting:
C#
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.
Posted
Updated 5-Mar-20 5:59am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900