您的代码适合我 - 通过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'如何根据两个键来选择给定的值。 –