Question

I've been working on a project lately and I've come up across this requirement, I have a collection of Foo items, like so:

public class Foo
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
    public string Prop4 { get; set; }
}


ObservableCollection<Foo> Foolist = new ObservableCollection<Foo>();

Now I know I can for example group the Foo items in the collection using any of the properties like this:

var groupedItems = Foolist.GroupBy(x => x.Prop1);

What I need to do though is to actually select distinct instances of a property (say Prop2) and group them using another property (say Prop1).. any help doing this would be appreciated.

Was it helpful?

Solution

You want something like this

Foolist.GroupBy(x => x.Prop2).Select(x => x.First()).GroupBy(x => x.Prop1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top