I am looking for efficient way to combine multiple hashsets (based on object key) while preserving non key values (such as Version below).
class MyObject {
public string Key {get; set;}
public long Version {get; set;}
override GetHashCode() { *key* }
override Equals(...) { *key* }
}
in the and, i need to combine hash sets into a master list, but also all Versions.
I can make the union of 3 sets in this way:
for (var o in List1.Union(List2).Union(List3))
Console.WriteLine("{0} : {1}", o.Key, o.Version)
this only shows the versions from one of the lists (List1, or whatever list contains the item).
I need to compile these into a result with all versions..
Something i wish i could do like this:
for (var o in List1.Union(List2).Union(List3).Select((a,b,c) => new DiffObj(){Key=a.Key,VersionA=a.Version,VersionB=b.Version,VersionC=c.Version}))
Console.WriteLine("{0} : {1},{2},{3}", o.Key, o.VrsionA, o.VersionB, VersionC);
is that possible with hashsets?
update
it is important to keep track which list had which version (in the final result).
It sounds like you want a grouping:
Then you can just iterate over each group, which will contain equal values according to
EqualsandGetHashCode, but which can still be distinct. TheListvalue will indicate which list each item came from.