如何在 C# 中重写列表的“包含”属性



我有一个对象列表List<IReportRelationMapping>我需要检查该列表是否不包含特定的ReportRelationMapping对象

这是我ReportRelationMapping的样子

public class ReportRelationMapping : IReportRelationMapping
{
     public string Name { get; set; }
     public IReportRelation LocalRelation { get; set; }
     public IReportRelation ForeignRelation { get; set; }
     public IReportRelationMapping RelatedThrough { get; set; }
}

如果this.LocalRelation == passed.LocalRelation && this.ForeignRelation == passed.ForeignRelationthis.LocalRelation == passed.ForeignRelation && this.ForeignRelation == passed.LocalRelation,则列表包含对象

这是我为了覆盖列表的Contains属性所做的

public class ReportRelationMapping : IReportRelationMapping
{
    public string Name { get; set; }
    public IReportRelation LocalRelation { get; set; }
    public IReportRelation ForeignRelation { get; set; }
    public IReportRelationMapping RelatedThrough { get; set; }
    public bool Equals(ReportRelationMapping other)
    {
        if (other == null)
        {
            return false;
        }
        if (object.ReferenceEquals(this, other))
        {
            return true;
        }
        if (this.GetType() != other.GetType())
        {
            return false;
        }
        return (this.LocalRelation == other.LocalRelation && this.ForeignRelation == other.ForeignRelation)
            || (this.LocalRelation == other.ForeignRelation && this.ForeignRelation == other.LocalRelation);
    }
    public override bool Equals(object other)
    {
        if (other == null)
        {
            return false;
        }
        if (object.ReferenceEquals(this, other))
        {
            return true;
        }
        if (this.GetType() != other.GetType())
        {
            return false;
        }
        return this.Equals(other as ReportRelationMapping);
    }
    public override int GetHashCode()
    {
        int hash = 14;
        hash = (hash * 7) + this.ForeignRelation.GetHashCode();
        return (hash * 7) + this.LocalRelation.GetHashCode();
    }
    public static bool operator ==(ReportRelationMapping lhs, ReportRelationMapping rhs)
    {
        // Check for null on left side. 
        if (Object.ReferenceEquals(lhs, null))
        {
            if (Object.ReferenceEquals(rhs, null))
            {
                // null == null = true. 
                return true;
            }
            // Only the left side is null. 
            return false;
        }
        // Equals handles case of null on right side. 
        return lhs.Equals(rhs);
    }
    public static bool operator !=(ReportRelationMapping lhs, ReportRelationMapping rhs)
    {
        return !(lhs == rhs);
    }
}

但出于某种原因,即使列表包含给定对象,我也会收到false或"不包含对象"。 当我调试代码时,我可以看到调试器到达Equal方法,因此它通过我的代码,但永远不会到达GetHashCode方法。我不确定我是否在这里错误地实现了我的GetHashCode方法。

我在这里错过了什么?为什么在我的情况下,包含总是返回"不包含"?如何正确覆盖列表的此Contains方法?

您可能应该通过调用 Enumerable.Any() 来执行此操作,如下所示:

bool contains = myList.Any(t =>
    (t.LocalRelation == passed.LocalRelation && t.ForeignRelation == passed.ForeignRelation)
    || (t.LocalRelation == passed.ForeignRelation && t.ForeignRelation == passed.LocalRelation)

无需覆盖Equals或任何这些。

或者,如果您不想使用 Enumerable (LINQ),可以使用 List.Exists 方法,该方法以相同的方式调用:

bool contains = myList.Exists(t =>
    (t.LocalRelation == passed.LocalRelation && t.ForeignRelation == passed.ForeignRelation)
    || (t.LocalRelation == passed.ForeignRelation && t.ForeignRelation == passed.LocalRelation)

最新更新