Самый простой способ - сделать свой собственный EqualityComparer
, хотя, вероятно, это будет не самый эффективный подход
Пример
public class MyComparer : IEqualityComparer<HashSet<int>>
{
public bool Equals(HashSet<int> x, HashSet<int> y)
=> x?.SetEquals(y) ?? false;
public int GetHashCode(HashSet<int> obj)
{
unchecked
{
return obj.Aggregate(17, (current, item) => current * 31 + item.GetHashCode());
}
}
}
Использование
var rand = new Random();
var hashes = Enumerable.Range(0, 20)
.Select(x => new HashSet<int>(Enumerable.Range(0, 3)
.Select(y => rand.Next(0, 5))));
var hashList = new HashSet<HashSet<int>>(hashes, new MyComparer()) ;
foreach (var list in hashList)
Console.WriteLine(string.Join(", ",list));
Выход
2, 0
3, 1
0, 1, 4
0, 2, 1
0, 3
4, 0, 3
4, 3
0, 1
1, 2
4, 3, 1
2, 1, 0
2
3, 1, 4
3, 2
4, 1, 3
0, 2
1, 4
1, 3
Полная демонстрация здесь