我仅获取不重复列表有问题。
所以我有 :
List<List<int>> combinations = new List<List<int>>();
combinations.Add(new List<int> {1,1});
combinations.Add(new List<int> {1,2});
combinations.Add(new List<int> {1,1}); // Same
combinations.Add(new List<int> {1,3});
我需要做的只是获得:
{1,1}
{1,2}
{1,3}
我尝试过此方法:combinations = combinations.Distinct().ToList();
但这不起作用。
有任何想法吗。先感谢您。
您可以使用自己的比较器:
var distincts =
combinations
.Distinct(new ListOfIntComparer());
class ListOfIntComparer : IEqualityComparer<List<int>>
{
public bool Equals(List<int> a, List<int> b)
{
return
a.SequenceEqual(b);
}
public int GetHashCode(List<int> l)
{
unchecked
{
int hash = 19;
foreach (var foo in l)
{
hash = hash * 31 + foo.GetHashCode();
}
return hash;
}
}
}
GetHasCode()
Jon Skeet的实现在这里。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句