Union returns Distinct values. By default, it will compare item references. Your items have different links, so they are all considered different. When you click on the base type X link does not change.
If you override Equals and GetHashCode (used to select individual elements), then the elements will not be compared by reference:
class X { public int ID { get; set; } public override bool Equals(object obj) { X x = obj as X; if (x == null) return false; return x.ID == ID; } public override int GetHashCode() { return ID.GetHashCode(); } }
But all your items have different ID values. Thus, all items are still considered different. If you provide multiple elements with the same ID , then you will see the difference between Union and Concat :
var lstX1 = new List<X1> { new X1 { ID = 1, ID1 = 10 }, new X1 { ID = 10, ID1 = 100 } }; var lstX2 = new List<X2> { new X2 { ID = 1, ID2 = 20 },
Your original pattern works because integers are value types and they are compared by value.
Sergey Berezovskiy Nov 16 '12 at 13:29 2012-11-16 13:29
source share