具有不同对象实例的列表上的并集/除外

本文关键字:除外 列表 对象 实例 | 更新日期: 2023-09-27 18:20:57

是否可以对对象列表执行并集/except,其中对象实例不一定相同,但功能等效?

我的意思是,如果我有这样的课,

Class A
{
    String a;
    int b;
    double c;
}

我有以下清单:

A foo = new A() {"a",2,3.4}    
A bar = new A() {"a",2,3.4}
List<A> firstList = new List<A>() { foo }
List<A> secondList = new List<A>() { bar }

如果firstList和secondList有完全不同的对象实例,但对象的字段/属性完全相同,我如何在secondList上执行firstList.Except/Union?

具有不同对象实例的列表上的并集/除外

您需要重载类的Equals方法。

现在,它检查相等性的方法是通过检查引用。有一种方法可以解决这个问题,通过重写Equals方法:

class A
{
    string a;
    int b;
    double c;
    public override bool Equals(object obj)
    {
        A aobj = obj as A;
        if (aobj == null) return false;
        return a == aobj.a && b == aobj.b && c == aobj.c;
    }
}

但是,要使这些函数发挥最佳性能,还需要重写GetHashCode方法。像这样:

class A
{
    string a;
    int b;
    double c;
    public override bool Equals(object obj)
    {
        return a == obj.a && b == obj.b && c == obj.c;
    }
    public override int GetHashCode()
    {
        unchecked { return 17 * (a ?? "").GetHashCode() * b * c.GetHashCode(); }
    }
}

只需重写object.Equals方法,即可告诉世界何时平等对待对象。你的class A应该是这样的:

class A
{
    string a;
    int b;
    double c;
    public override bool Equals(object obj)
    {
        if (!(obj is A)) return obj.Equals(this); // defer to other object
        A other = (A)obj;
        return a == other.a && b == other.b && c == other.c; // check field equality
    }
    public override int GetHashCode()
    {
        int hc = 13;
        hc += a.GetHashCode() * 27;
        hc += b.GetHashCode() * 27;
        hc += c.GetHashCode() * 27;
    }
}

在前面的回答中添加了一些内容。重写Equals需要重写==和!=

public class A
{
    String a;
    int b;
    double c;
    public override bool Equals(object obj)
    {   
        if (object.ReferenceEquals(null, obj))
        {
            return false;
        }
        if (object.ReferenceEquals(this, obj))
        {
            return true;
        }
        if (obj.GetType() != typeof(A))
        {
            return false;
        }
        var other = obj as A;
        return string.Equals(this.a, other.a) && this.b == other.b && this.c == other.b;
    }
    public override int GetHashCode()
    {
        if (string.IsNullOrEmpty(a))
        {
            return this.b.GetHashCode() ^ this.c.GetHashCode();
        }
        return this.a.GetHashCode() ^ this.b.GetHashCode() ^ this.c.GetHashCode();
    }
    public static bool operator ==(A left, A right)
    {
        if (object.ReferenceEquals(left, right))
        {
            return true;
        }
        if (object.ReferenceEquals(null, left))
        {
            return false;
        }
        if (object.ReferenceEquals(null, right))
        {
            return false;
        }
        return left.Equals(right);
    }
    public static bool operator !=(A left, A right)
    {
        return !(left == right);
    }
}

您可以使用LINQ to Objects在列表上创建交集和并集,而无需重写Equals和GetHashCode。使用Enumerable.Intersect和Enumerable.Except方法:

public class A
{
    public String a;
    public int b;
    public double c;
}
A foo = new A() {a = "a", b = 2, c = 3.4};
A bar = new A() {a = "a", b = 2, c = 3.4};
List<A> firstList = new List<A>() { foo };
List<A> secondList = new List<A>() { bar };
firstList.Except(secondList);
firstList.Intersect(secondList);

这种情况下的输出是:

same entries: 
>> IEnumerable<A> (1 item) 4  
>> a b c 
>> a 2 3,4 

您可以组合firstList.Method(secondsList),反之亦然。您可以事件编写一个自定义的比较器-IEqualityComparer来比较复杂的对象类型。