C# 使用IEquatable进行对象比较失败
我有实现IEquatable的自定义对象,这样我就可以使用List.Contains(另一个CustomObject)。因此,如果它包含对象,请将其从列表中删除 问题是我的implemented.Equals没有像预期的那样工作,还有一些但是现在所有的都显示为不相等,并且没有从列表中删除 为了澄清,在currentSpecs下面的图像中是我想要从中删除的列表,如果它包含tmpAS400Specs中的任何内容 下面是代码C# 使用IEquatable进行对象比较失败,c#,C#,我有实现IEquatable的自定义对象,这样我就可以使用List.Contains(另一个CustomObject)。因此,如果它包含对象,请将其从列表中删除 问题是我的implemented.Equals没有像预期的那样工作,还有一些但是现在所有的都显示为不相等,并且没有从列表中删除 为了澄清,在currentSpecs下面的图像中是我想要从中删除的列表,如果它包含tmpAS400Specs中的任何内容 下面是代码 var tmpSpecs = new AS400Specification
var tmpSpecs = new AS400SpecificationAttribute
{
ProductNumber = workTask.WIITEM,
AttributeGroup = attrGroup,
AttributeName = attrName,
AttributeValue = attrValue
};
if (currentSpecs.Contains(tmpSpecs))
{
currentSpecs.Remove(tmpSpecs);
}
比较代码
public class AS400SpecificationAttribute : IEquatable<AS400SpecificationAttribute>
{
private string name;
private string value;
private string group;
private string productNumber;
public string ProductNumber
{
get { return productNumber; }
set
{
if (!String.IsNullOrEmpty(value) && !String.IsNullOrWhiteSpace(value))
productNumber = value.Trim().ToUpper();
}
}
public string AttributeName
{
get
{
return this.name;
}
set
{
if (!String.IsNullOrEmpty(value) && !String.IsNullOrWhiteSpace(value))
this.name = value.Trim();
}
}
public string AttributeValue
{
get
{
return this.value;
}
set
{
if (!String.IsNullOrEmpty(value) && !String.IsNullOrWhiteSpace(value))
this.value = value.Trim().Replace("\\\"","\"");
}
}
public string AttributeGroup
{
get
{
return this.group;
}
set
{
if (!String.IsNullOrEmpty(value) && !String.IsNullOrWhiteSpace(value))
this.group = value.Trim();
}
}
public bool Equals(AS400SpecificationAttribute other)
{
if (other == null)
return false;
return this.ProductNumber.Equals(other.productNumber)
&& ((this.group != null && this.group.Equals(other.AttributeGroup))
|| (this.group == null && other.AttributeGroup == null))
&& ((this.name!= null && this.name.Equals(other.AttributeName))
|| (this.name == null && other.AttributeName == null))
&& ((this.value != null && this.value.Equals(other.AttributeValue))
|| (this.value == null && other.AttributeName == null));
}
}
公共类AS400SpecificationAttribute:IEquatable
{
私有字符串名称;
私有字符串值;
私有字符串组;
私有字符串productNumber;
公共字符串ProductNumber
{
获取{return productNumber;}
设置
{
如果(!String.IsNullOrEmpty(value)&&&!String.IsNullOrWhiteSpace(value))
productNumber=value.Trim().ToUpper();
}
}
公共字符串属性名
{
得到
{
返回此.name;
}
设置
{
如果(!String.IsNullOrEmpty(value)&&&!String.IsNullOrWhiteSpace(value))
this.name=value.Trim();
}
}
公共字符串AttributeValue
{
得到
{
返回此.value;
}
设置
{
如果(!String.IsNullOrEmpty(value)&&&!String.IsNullOrWhiteSpace(value))
this.value=value.Trim();
}
}
公共字符串属性组
{
得到
{
返回此.group;
}
设置
{
如果(!String.IsNullOrEmpty(value)&&&!String.IsNullOrWhiteSpace(value))
this.group=value.Trim();
}
}
公共布尔等于(AS400SpecificationAttribute其他)
{
如果(其他==null)
返回false;
返回此.ProductNumber.Equals(其他.ProductNumber)
&&((this.group!=null&&this.group.Equals(other.AttributeGroup))
||(this.group==null&&other.AttributeGroup==null))
&&((this.name!=null&&this.name.Equals(other.AttributeName))
||(this.name==null&&other.AttributeName==null))
&&((this.value!=null&&this.value.Equals(other.AttributeValue))
||(this.value==null&&other.AttributeName==null));
}
}
为什么比较不能检测到这两个属性是相同的
public override bool Equals(object obj)
{
var other = obj as AS400SpecificationAttribute;
if (other == null)
。。。等等。我注意到:
Object A.value='x',Object B.value='x'。
尝试使其不区分大小写,或者确保对象真正相等 看起来你缺少了一些括号,内括号有点像这样:
((a&&b)| c&&d)
难道c&&d
也应该在括号内吗((this.group!=null&&this.group.Equals(other.AttributeGroup))| | this.group==null&&other.AttributeGroup==null)
您能给我们展示至少属性实现的完整类吗?@LasseV.Karlsen True已修复但不是主要问题,仍然遇到错误。@AlessandroD'Andria是的,先生!尝试使其不区分大小写。对象A.value='x',对象B.value='x'List
在实现时使用IEquatable
,因为它使用EqualityComparer.Default
。我想最好详细说明这个答案。