2017-05-11 104 views
0

我有2個屬性比較平等的2個Properties對象

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; } 
} 

我已創建並添加到列表這個類的一個實例

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"; 

這裏是奇怪的部分: 如果我比較列表中的屬性名稱與Sample類的第二個實例與同一類:

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

結果是真實的,但如果 我比較列表屬性

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

結果是假的?! 有人可以提供一些關於這種行爲的信息。

回答

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))); 
    } 
} 
+0

還有其他選項,例如序列化兩個對象並比較結果字符串或使用反射來比較對象屬性 –

0

第一個比較是關於比較兩個字符串的值。然而,第二個比較是關於比較兩個不同的對象,它們的引用是不同的。事實上,對於第二次比較,比較它們的hashCode。要觀看此視頻,您可以撥打.GetHashCode()這兩個對象。

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

好,但如何讓「正確」的對象,然後才能得到真正的結果,如果對象是相同的比較? – NDym

+0

正確的比較是按成員比較這些字段的成員。此外,還有不同的技術以乾淨的形式做到這一點。例如,你可以看到這篇文章:http://stackoverflow.com/questions/10454519/best-way-to-compare-two-complex-object – OmG

+0

請記住,哈希代碼只告訴你,2個對象是絕對不同的,如果他們不匹配...你不能從匹配的哈希碼中推斷出相等(或任何東西)。我以艱難的方式學會了這一點,並相信我,忽略它會導致一些難以調試的故障。 –