[英]Efficient way to group all equal objects defined by IEqualityComparer
I have a big list of certain objects that are of type Order
. 我列出了
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. 此外,我已经定义了
IEqualityComparer<Order>
,我的目标是有效地创建列表(或分组)的列表,其中每个子列表都包含彼此相等的对象。 Mathematically, I have an equivalence relation (IEqualityComparer) and want to have a list of all equivalence classes . 在数学上,我有一个等价关系 (IEqualityComparer),并希望具有所有等价类的列表。 I thought about using
HashSet<Order>
, Dictionary<>
or Lookup<>
but they dont seem to be perfect for my case. 我考虑过使用
HashSet<Order>
, Dictionary<>
或Lookup<>
但它们似乎并不适合我的情况。 So any ideas? 有什么想法吗?
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))));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.