比较equals对象的2个属性

时间:2017-05-11 12:09:31

标签: c# .net linq

我有一个有2个属性的类

public class SampleClass
{
    public string Name { get; set; }
    public List<Component> Components { get; set; }
}

另一个持有一些字符串属性的类。

public class Component
{
    public string Name { get; set; }
    public string Age{ get; set; }
}

我已创建此类的实例并将其添加到List

SampleClass classWithValues = new SampleClass();
var listComponent = new List<Component>();
listComponent.add(new Component{Name = "Random string",Age = "31"})
classWithValues.Components = listComponent; 
classWithValues.Name = "TestName"

var listWithObjectClass = new List<SampleClass>();
listWithObjectClass.add(classWithValues);

然后我创建了一个SampleClass类的新实例,并在属性中添加了完全相同的值:

SampleClass classWithValues1 = new SampleClass();
var listComponent1 = new List<Component>();
listComponent1.add(new Component{Name = "Random string",Age = "31"})
classWithValues1.Components = listComponent1; 
classWithValues1.Name = "TestName";

这是一个奇怪的部分: 如果我将列表中的属性Name与Sample类的第二个实例与同一个类的新实例进行比较:

 bool alreadyExists = listWithObjectClass.Any(x => x.Name == classWithValues1 .Name);

结果是真的但是 如果我比较列表属性

 bool alreadyExists = listWithObjectClass.Any(x => x.Components == classWithValues1.Components);

结果是假的?! 有人可以提供一些有关此行为的信息。

2 个答案:

答案 0 :(得分:0)

第一个比较是关于比较两个字符串的值。然而,第二个比较是关于比较它们的参考不同的两个不同的对象。实际上,对于第二次比较,比较他们的hashCode。要观看此内容,您可以为这两个对象调用.GetHashCode()

 listComponent.GetHashCode() == listComponent1.GetHashCode() // false
 listComponent[0].GetHashCode() == listComponent1[0].GetHashCode() // false

答案 1 :(得分:0)

抱歉,我的第一个答案不太正确......

为了使alreadyExist成为真,你需要在类中进行属性比较,否则执行的相等比较是默认的引用比较。您的对象包含相同的属性值,但实际上是不同的实例...对象的默认相等比较是比较引用而不是内容。

试试这个......

void Main()
{
    SampleClass classWithValues = new SampleClass();
    var listComponent = new Components();
    listComponent.Add(new Component{Name = "Random string",Age = "31"});
    classWithValues.Components = listComponent; 
    classWithValues.Name = "TestName";

    var listWithObjectClass = new List<SampleClass>();
    listWithObjectClass.Add(classWithValues);

    SampleClass classWithValues1 = new SampleClass();
    var listComponent1 = new Components();
    listComponent1.Add(new Component{Name = "Random string",Age = "31"});
    classWithValues1.Components = listComponent1; 
    classWithValues1.Name = "TestName";

    bool alreadyExists = listWithObjectClass.Any(x => x.Components.Equals(classWithValues1.Components));
}

public class SampleClass
{
    public string Name { get; set; }
    public Components Components { get; set; }
}

public class Component : IEquatable<Component>
{
    public string Name { get; set; }
    public string Age{ get; set; }

    public bool Equals(Component otherComponent)
    {
        return Name == otherComponent.Name && Age == otherComponent.Age;
    }
}

public class Components :List<Component>, IEquatable<Components>
{
    public bool Equals(Components otherComponents)
    {
        if(this.Count!= otherComponents.Count) return false;

        return this.TrueForAll(a=> otherComponents.Any(q=>q.Equals(a)))
        && otherComponents.TrueForAll(a=> this.Any(q=>q.Equals(a)));
    }
}