首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么使用Distinct()去除重复数组

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

如何使用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();
           

热点排行