How to get a distinct list from a List of objects?
Unfortunately there's no really easy built-in support for this in the framework - but you can use the DistinctBy
implementation I have in MoreLINQ.
You'd use:
var distinctList = someList.DistinctBy(x => x.Prop2).ToList();
(You can take just the DistinctBy
implementation. If you'd rather use a Microsoft implementation, I believe there's something similar in the System.Interactive assembly of Reactive Extensions.)
You can emulate the effect of DistinctBy
using GroupBy
and then just using the first entry in each group. Might be a bit slower that the other implementations though.
someList.GroupBy(elem=>elem.Prop2).Select(group=>group.First());