Reputation: 994
I have the following dynamic object as IEnumerable of type dynamic
[{
"id": 1,
"subList": [
{"specialId": 42},
{"specialId": 27}
]
},
{
"id": 2,
"subList": [
{"specialId": 13},
{"specialId": 14}
]
}]
I can get the objects into a IEnumerable of Dynamics and can run linq queries like the following
listOfDynamics.Where(x => x.id == 2);
However what I would like to do is be able to do is match on the subList
listOfDynamics.Where(x => ((IEnumerable)x.subList)).Where(y => y.specialId == 42));
so in the example above it would return the object with id of 1 but not the id of 2
Upvotes: 2
Views: 715
Reputation: 43876
In your Where
you want to check if the subList
contains any element with specialId == 42
:
listOfDynamics.Where(x => ((IEnumerable<dynamic>)x.subList).Any(y => y.specialId == 42));
So Any()
is the method you want.
And you need to cast x.subList
to IEnumerable<dynamic>
instead of only IEnumerable
(as suggested by @Ivan-Stoev and @Derked in the comments).
Upvotes: 4