0

我误解了如何实现 EqualityComparer 和 Intersect。我想了解使用 c# 和 linq 的集合,但下面的代码不会产生任何结果。这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{



public class objTest : EqualityComparer<objTest>
{
    public int index { get; set; }
    public string name { get; set; }

    public override  bool Equals(objTest x, objTest y)
    {
        return x.index == y.index;

    }
    public override int GetHashCode(objTest obj)
    {

        return obj.index.GetHashCode();
    }


}
 static void Main(string[] args)
{

    List<objTest> L1 = new List<objTest>();
    L1.Add(new objTest { index = 1, name = "ALAN" });
    L1.Add(new objTest { index = 2, name = "bill" });
    L1.Add(new objTest { index = 3, name = "clive" });
    L1.Add(new objTest { index = 4, name = "dave" });
    L1.Add(new objTest { index = 5, name = "ewan" });

    List<objTest> L2 = new List<objTest>();
    L2.Add(new objTest { index = 11, name = "ALAN" });
    L2.Add(new objTest { index = 12, name = "bill" });
    L2.Add(new objTest { index = 13, name = "clive" });
    L2.Add(new objTest { index = 14, name = "dave" });
    L2.Add(new objTest { index = 1, name = "ALAN2" });
    //THIS HAS NO RESULTS
    var L3 = L1.Intersect(L2);

}
}

有人能告诉我为什么 L3 没有结果吗?

4

3 回答 3

1

您需要引用在 中实现的类的IEqualityComparer实例Intersect。像这样:

        public class objTest 
        {
            public int index { get; set; }
            public string name { get; set; }
        }

        class eqcomp : EqualityComparer<objTest>
        {
            public override bool Equals(objTest x, objTest y)
            {
                return x.index == y.index;

            }
            public override int GetHashCode(objTest obj)
            {

                return obj.index.GetHashCode();
            }


        }

        static void Main(string[] args)
        {
            List<objTest> L1 = new List<objTest>();
            L1.Add(new objTest { index = 1, name = "ALAN" });
            L1.Add(new objTest { index = 2, name = "bill" });
            L1.Add(new objTest { index = 3, name = "clive" });
            L1.Add(new objTest { index = 4, name = "dave" });
            L1.Add(new objTest { index = 5, name = "ewan" });

            List<objTest> L2 = new List<objTest>();
            L2.Add(new objTest { index = 11, name = "ALAN" });
            L2.Add(new objTest { index = 12, name = "bill" });
            L2.Add(new objTest { index = 13, name = "clive" });
            L2.Add(new objTest { index = 14, name = "dave" });
            L2.Add(new objTest { index = 1, name = "ALAN2" });
            //THIS HAS RESULTS :)
            var L3 = L1.Intersect(L2, new eqcomp());
     }
}
于 2013-10-02T16:40:34.507 回答
1

如果您想使用外部比较,mrtig 已经回答了。

如果您希望objTest实例相互比较: 您正在覆盖错误的Equalsand GetHashCode。您应该覆盖Equals接受一个参数并将其与this. 您不应该实施EqualityComparer.

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    public class objTest
    {
        public int index { get; set; }
        public string name { get; set; }

        public override bool Equals(object other)
        {
            objTest o = other as objTest;
            return o != null && o.index == index;
        }
        public override int GetHashCode()
        {
            return index.GetHashCode();
        }
    }
    static void Main(string[] args)
    {
        List<objTest> L1 = new List<objTest>();
        L1.Add(new objTest { index = 1, name = "ALAN" });
        L1.Add(new objTest { index = 2, name = "bill" });
        L1.Add(new objTest { index = 3, name = "clive" });
        L1.Add(new objTest { index = 4, name = "dave" });
        L1.Add(new objTest { index = 5, name = "ewan" });

        List<objTest> L2 = new List<objTest>();
        L2.Add(new objTest { index = 11, name = "ALAN" });
        L2.Add(new objTest { index = 12, name = "bill" });
        L2.Add(new objTest { index = 13, name = "clive" });
        L2.Add(new objTest { index = 14, name = "dave" });
        L2.Add(new objTest { index = 1, name = "ALAN2" });
        //THIS HAS NO RESULTS
        var L3 = L1.Intersect(L2);
    }
}
于 2013-10-02T16:39:58.537 回答
0

您应该实施IEquatable<objTest>而不是IEqualityComparer<objTest>. MSDN 文档Intersect具有误导性,因为它指示您实现后者,即使示例实现了前者。

请注意,在这种情况下,您还应该覆盖Object.Equalsand 。Object.GetHashCode就像是:

public bool Equals(objTest other)
{
    return this.index == other.index;
}

public override bool Equals(object other)
{
    var o = other as objTest;

    return o != null && Equals(o);
}

public override int GetHashCode()
{
    return index.GetHashCode();
}
于 2013-10-02T16:45:00.947 回答