Efficient way to group all equal objects defined by IEqualityComparer

84 views Asked by At

I have a big list of certain objects that are of type Order. Furthermore, I have defined an IEqualityComparer<Order> and my goal is to efficiently create a list of lists (or groupings) where every sublist contains the objects that are equal to each other. Mathematically, I have an equivalence relation (IEqualityComparer) and want to have a list of all equivalence classes. I thought about using HashSet<Order>, Dictionary<> or Lookup<> but they dont seem to be perfect for my case. So any ideas?

1

There are 1 answers

0
0xBADF00D On

I think this something like this shluod to the job:

var uniqueItems = yourRawList.Distinct(yourComparer);
var result = uniqueItems.Select(i => new List<Order>(yourRawList.Where(o =>yourComparer.Equals(i, o))));