如何覆盖 C# 中列表的 Contains 属性?
How to override Contains property of a list in 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.ForeignRelation
或 this.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
或 "does not contains the object." 当我调试我的代码时,我可以看到调试器到达 Equal
方法,所以它通过我的代码,但它永远不会到达 GetHashCode
方法。我不确定我是否在此处错误地实施了我的 GetHashCode
方法。
我错过了什么?为什么在我的例子中总是包含 return "Not Contain"?
如何正确覆盖列表的 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)
我有一个对象列表 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.ForeignRelation
或 this.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
或 "does not contains the object." 当我调试我的代码时,我可以看到调试器到达 Equal
方法,所以它通过我的代码,但它永远不会到达 GetHashCode
方法。我不确定我是否在此处错误地实施了我的 GetHashCode
方法。
我错过了什么?为什么在我的例子中总是包含 return "Not Contain"?
如何正确覆盖列表的 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)