日期:2014-05-17  浏览次数:20906 次

如何使用Distinct()去除重复数组?
有一个数组列表rec,如:            
rec.Add(new int[] { 1, 2, 3, 4, 5 });
rec.Add(new int[] { 1, 2, 3, 5, 6 });
rec.Add(new int[] { 1, 2, 3, 4, 5 });

如何用Distinct()去除rec中重复的数组(0和2重复)。知道要重写distinct()里Comparer函数,但不知道该如何重写,求代码

------解决方案--------------------
class ComparerClass : System.Collections.Generic.IEqualityComparer<int[]>
        {
            public bool Equals(int[] a, int[] b)
            {
                if (a == null && b == null)
                    return true;
                else if (a != null && b != null)
                {
                    if (a.Length != b.Length)
                        return false;
                    a = a.OrderBy(t => t).ToArray();
                    b = b.OrderBy(t => t).ToArray();
                    for (int i = 0; i < a.Length; i++)
                        if (a[i] != b[i])
                            return false;
                    return true;
                }
                else return false;
            }
            public int GetHashCode(int[] ary)
            {
                return base.GetHashCode();
            }
        }

// rec = rec.Distinct(new ComparerClass()).ToList();