How to use LINQ with dynamic collections

Is there a way to convert a dynamic object into an IEnumerable Type to filter the collection using a property.

 dynamic data = JsonConvert.DeserializeObject(response.Content); 

I need to access something like this

 var a = data.Where(p => p.verified == true) 

Any ideas?

+43
c # linq
Sep 11 '13 at 7:20
source share
2 answers

As long as data is an IEnumerable some type, you can use:

 var a = ((IEnumerable) data).Cast<dynamic>() .Where(p => p.verified); 

Cast<dynamic>() must end with IEnumerable<dynamic> , so the parameter type for the lambda expression is also dynamic .

+76
Sep 11 '13 at 7:22
source share

Try casting to IEnumerable<dynamic>

 ((IEnumerable<dynamic>)data).Where(d => d.Id == 1); 

This approach is 4 times faster than other approaches.

luck

+26
Sep 11 '13 at 7:22
source share



All Articles