我有一个string []列表:
List<string[]> EIndex;
Eindex列表有4个string[]
INPUT: {0,John},{1,Mike},{2,John},{3,Tim}
Expected OUTPUT: {0,John},{1,Mike},{3,Tim}
我想基于string[]
的索引编号1区分数组,如:
List<string[]> DistinctList = Eindex.Distinct(obj => obj[1]).ToList();
任何建议?
答案 0 :(得分:0)
Distinct
采用IEqualityComparer<T>
参数。您可以实现自定义比较器,以使您的数组与第二个索引处的项目等同:
public class SecondIndexComparer : IEqualityComparer<string[]>
{
public bool Equals(string[] x, string[] y)
{
return x[1] == y[1];
}
public int GetHashCode(string[] obj)
{
return obj[1].GetHashCode();
}
}
并像这样使用它:
var distinctList = Eindex.Distinct(new SecondIndexComparer()).ToList();
请参阅此处的工作示例:https://dotnetfiddle.net/cpv1i7