How to use LINQ with dynamic collections
Is there a way to convert dynamic
object to IEnumerable
Type to filter collection with property.
dynamic data = JsonConvert.DeserializeObject(response.Content);
I need to access something like this
var a = data.Where(p => p.verified == true)
Any Ideas?
So long as data
is an IEnumerable
of some kind, you can use:
var a = ((IEnumerable) data).Cast<dynamic>()
.Where(p => p.verified);
The Cast<dynamic>()
is to end up with an IEnumerable<dynamic>
so that the type of the parameter to the lambda expression is also dynamic
.
Try casting to IEnumerable<dynamic>
((IEnumerable<dynamic>)data).Where(d => d.Id == 1);
This approach is 4x faster than other approachs.
good luck