Intersect未使用EqualityComparer提供任何结果

时间:2013-10-02 16:32:34

标签: c# .net linq

我误解了如何实现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没有结果吗?

3 个答案:

答案 0 :(得分:1)

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

如果您希望objTest个实例进行自我比较: 您覆盖了错误的EqualsGetHashCode。您应该覆盖带有一个参数的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);
    }
}

答案 1 :(得分: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());
     }
}

答案 2 :(得分:0)

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

请注意,在这种情况下,您还应该覆盖Object.EqualsObject.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();
}