Linqs是“.Distinct()”隐隐绰绰

时间:2012-07-30 20:30:48

标签: c# linq collections filter abstraction

using System.Linq;

var a = new byte?[][,]{
    new byte?[,]class Nullable_Byte_2D_Array_EqualityComparer : IEqualityComparer<byte?[,]>
{
    public bool Equals(byte?[,] a, byte?[,] b)
    {
        var r = a.GetLength(0) == b.GetLength(0) &&
            a.GetLength(1) == b.GetLength(1);
        if (r)
        {
            var v = new byte?[a.Length];
            byte n = 0;
            foreach (byte? c in a)
            {
                v[n] = c;
                n++;
            }
            n = 0;
            foreach (byte? c in b)
            {
                if (c != v[n])
                    r = false;
                n++;
            }
        }
        return r;
    }
}
,
    new byte?[,]a = a.Distinct(new Nullable_Byte_2D_Array_EqualityComparer()).ToArray();
,
    new byte?[,]{{2}},
    new byte?[,]{{1, 2, 3}, {4, 5, 6}}};
a = a.Distinct().ToArray();

然而'a'仍然包含重复。我这样做了吗?


感谢。从答案中的信息。

{{1}}

{{1}}

1 个答案:

答案 0 :(得分:13)

它不包含重复内容。它包含两个不同的数组,它们内部恰好具有相同的值。由于数组是引用类型,Distinct()默认情况下会进行引用比较;要更改此行为,请使用this override指定您自己的比较器。