I have an inconsistent JArray items like below, sometimes it has event property, but I need to count based on event value.
[
{
"id": 114792,
"attributes": {
"priority": 0
}
},
{
"id": 114792,
"attributes": {
"priority": 0,
"event": 10
}
},
{
"id": 114793,
"attributes": {
"priority": 0,
"event": 0
}
}
]
How to count based on criteria for example event = 0? I tried below:
_ja.Select(x => x.ToObject<JObject>()).Where(x => (int)x["attributes"]["event"] == 0).Count();
You are almost there. just add the null condition in the Where
.
Here is the working code
_ja.Select(x => x.ToObject<JObject>())
.Where(x => x["attributes"]["event"] != null && (int)x["attributes"]["event"] == 0).Count();