Search code examples
c#dynamictype

Get distinct objects from list of dynamic objects


I'm trying to return a distinct list of dynamic objects. The object that I am filtering on is of type dynamic {System.Collections.Generic.List<object>} with each of its indexed items being of type object {System.Dynamic.ExpandoObject}.

An example of the data structure is found below:

Model.Object
    - [0]
        - Property 1 (value: aaa)
        - Property 2 (value: 123)
        - Property 3 (value: a123)
    - [1]
        - Property 1 (value: bbb)
        - Property 2 (value: 456)
        - Property 3 (value: a456)
    - [2]
        - Property 1 (value: ccc)
        - Property 2 (value: 123)
        - Property 3 (value: a123)`

I have tried the following, but with no success: var distinctResults = ((List<object>)Model.Object).GroupBy(elem => elem.Property 2).Select(group => group.First());

Any help please?


Solution

  • As Lasse V. Karlsen said, cast it to List<dynamic>