Linked Questions

Popular Questions

Linq query question: Distinct based on object's property

Asked by At

I am aggregating multiple List's into one List and would like to make it distinct based on one of the properties of Foo (Foo.Prop1).. I do not have access to modify Foo's Equality comparer.

Dictionary<string, List<Foo>> fooDictionary = new Dictionary<string, List<Foo>>();
List<Foo> foovals = (from e in fooDictionary
                     where e.Key == "foo1" || e.Key == "foo2" || e.Key == "foo3"
                     select e.Value).SelectMany(f => f).ToList();

the only thing missing here is the .Distinct() at the end, to make Foo's unique, however, in my case, i can't modify anything about Foo, so simply calling Distinct() will not work.

Is there a way to modify this query to return items Distinct based on Foo.Prop1 ?

Related Questions