您的代碼適合我 - 通過ReferenceSource進行跟蹤,默認比較最終使用的是ObjectEqualityComparer,它調用Equals()
,所以您的想法是正確的。
所以這涉及到你如何實現Equals
和GetHashCode
。如果您覆蓋兩者,則應覆蓋兩者:如MSDN states:
注意:如果覆蓋GetHashCode方法,則還應該覆蓋Equals,反之亦然。如果重寫的Equals方法在兩個對象測試相等時返回true,則重寫的GetHashCode方法必須爲這兩個對象返回相同的值。
注意你的ID
類也需要正確地處理這兩種方法,它應該由Plan
被用來檢查平等和獲得的哈希碼。
這個程序爲我工作,只打印與ID=2
第二個條目(請注意,我做了SomeOtherProperty
和int
爲簡單起見,但這並不影響該方法或代碼):
class Program
{
public class Plan
{
public int ID { get; set; }
public Plan Parent { get; set; }
public int SomeOtherProperty { get; set; }
// added to show we don't care about this
public string IgnoreMe { get; set; }
public Plan(int id, int other, Plan parent, string ignore)
{
this.ID = id;
this.SomeOtherProperty = other;
this.Parent = parent;
this.IgnoreMe = ignore;
}
public override bool Equals(object obj)
{
Plan other = (Plan)obj;
// just check the relevant properties
return this.ID == other.ID
&& this.SomeOtherProperty == other.SomeOtherProperty
&& this.Parent == other.Parent;
// .. or alternatively
//return (new { ID, SomeOtherProperty, Parent })
// .Equals(new { other.ID, other.SomeOtherProperty, other.Parent });
}
// nicked from http://stackoverflow.com/a/4630550/1901857
public override int GetHashCode()
{
return new { ID, SomeOtherProperty, Parent }.GetHashCode();
}
// just to help debug
public override string ToString()
{
return string.Format("[ID: {0}, Other:{1}, Parent:{2}]", ID, SomeOtherProperty, Parent);
}
}
static void Main(string[] args)
{
var parentPlans = new Plan[] {
new Plan(101, 2, null, "parent1"),
new Plan(102, 3, null, "parent2"),
new Plan(103, 4, null, "parent3"),
new Plan(104, 5, null, "parent4")
};
List<Plan> oldPlans = new List<Plan>(new Plan[] {
new Plan(1, 2, parentPlans[0], "old1"),
new Plan(2, 3, parentPlans[1], "old2"),
new Plan(3, 4, parentPlans[2], "old3"),
new Plan(4, 5, parentPlans[3], "old4")
});
List<Plan> newPlans = new List<Plan>(new Plan[] {
new Plan(11, 2, parentPlans[0], "new1"), // different ID
new Plan(2, 3, parentPlans[1], "new2"), // same
new Plan(3, 14, parentPlans[2], "new3"), // different other ID
new Plan(4, 5, parentPlans[2], "new4") // different parent
});
foreach (var e in
oldPlans.Join(newPlans, o => o, n => n, (o, n) => new { Old = o, New = n }))
{
Console.WriteLine(e.Old + "/" + e.New);
};
}
}
如果你認爲你的Equals
和GetHashCode
的實現應該已經工作了,那麼請將它們發佈在問題中,也許它們不太正確。
您不必使用匿名對象。您只需創建一個'Func resultSelector',它將告訴'Enumerable.Join'如何根據兩個鍵來選擇給定的值。 –