I have a Dictionary of dictionaries :
SortedDictionary<int,SortedDictionary<string,List<string>>>
I would like to merge two dictionaries for example key = 2 and key = 3.
Very important I could have duplicate keys in the respective dictionaries.
example key = 2 has a dictionary Key,Value "1000",{a,b,c}
and key = 3 has a dictionary key,Value "1000",{z}.
So I would like to merge key 2 and key 3 and the result would be the following Sorted Dictionary key, Value "1000",{a,b,c,z}.
I am a beginner with LINQ syntax so could you help solve this with detail code..
Thanks
This is not a LINQ problem. Here’s a generic version that will solve your problem.
Usage:
Here’s how you approach a problem like this. First, specify what you’re trying to do.
Given a
SortedDictionary<TKey1, SortedDictionary<TKey2, List<TValue>>>and two keysintoKeyandfromKeyin the dictionary, merge the dictionary with keyfromKeyinto the dictionary with keyintoKey.Now specify what it means to merge two dictionaries. Given two dictionaries
toandfromof typeSortedDictionary<TKey2, List<TValue>>to merge them means the following. For eachTKey2 keyinfromthere are two possiblities:keyis into. In this case add the listfrom[key]to the listto[key].keyis not into. In this case addkeytotowith valuefrom[key].Then, remove key
fromKeyfrom the dictionary.Let’s translate this to code:
The rest of the code is just error checking