The only way that I can think of merging two SortedList
instances would be to union them, then convert to a lookup, then grab the first element of the lookup collection to make a dictionary.
I would need to make a dictionary because the SortedList
only supports one-by-one adds. So the only other option would be to inject a dictionary into the SortedList
constructor.
Bottom line: I think your current code is pretty decent as it is. LINQ can help reduce the code to about 2 lines (or one if you're a masochist).
SortedList<decimal, Traits> listA = new SortedList<decimal, Traits>();
SortedList<decimal, Traits> listB = new SortedList<decimal, Traits>();
listA.Add(1m, new Traits { FieldName = "One" });
listA.Add(2m, new Traits { FieldName = "Two" });
listA.Add(3m, new Traits { FieldName = "Three" });
listB.Add(1m, new Traits { FieldName = "One" });
listB.Add(4m, new Traits { FieldName = "Four" });
listB.Add(5m, new Traits { FieldName = "Five" });
var listUnion = listA.Union(listB).ToLookup(k => k.Key, v => v.Value)
.ToDictionary(k => k.Key, v => v.First());
var listMerged = new SortedList<decimal, Traits>(listUnion);