Reputation: 1231
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
Upvotes: 0
Views: 1831
Reputation: 241641
This is not a LINQ problem. Here's a generic version that will solve your problem.
static class SortedDictionaryExtensions {
public static void MergeKeys<TKey1, TKey2, TValue>(
this SortedDictionary<TKey1, SortedDictionary<TKey2, List<TValue>>> dictionary,
TKey1 intoKey,
TKey1 fromKey
) {
if (dictionary == null) {
throw new ArgumentNullException("dictionary");
}
if (intoKey == null) {
throw new ArgumentNullException("intoKey");
}
if (fromKey == null) {
throw new ArgumentNullException("fromKey");
}
SortedDictionary<TKey2, List<TValue>> to;
SortedDictionary<TKey2, List<TValue>> from;
if (!dictionary.TryGetValue(intoKey, out to)) {
throw new ArgumentOutOfRangeException("intoKey");
}
if (!dictionary.TryGetValue(fromKey, out from)) {
throw new ArgumentOutOfRangeException("fromKey");
}
foreach(TKey2 key in from.Keys) {
if (to.Keys.Contains(key)) {
to[key].AddRange(from[key]);
}
else {
to.Add(key, from[key]);
}
}
dictionary.Remove(fromKey);
}
}
Usage:
SortedDictionary<int, SortedDictionary<string, List<string>>> list =
new SortedDictionary<int, SortedDictionary<string, List<string>>>();
list.Add(2, new SortedDictionary<string, List<string>>());
list[2].Add("1000", new List<string>() { "a", "b", "c" });
list[2].Add("2000", new List<string>() { "b", "c" });
list.Add(4, new SortedDictionary<string, List<string>>());
list[4].Add("1000", new List<string>() { "z" });
list[4].Add("3000", new List<string>() { "y" });
list.MergeKeys(2, 4);
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 keys intoKey
and fromKey
in the dictionary, merge the dictionary with key fromKey
into the dictionary with key intoKey
.
Now specify what it means to merge two dictionaries. Given two dictionaries to
and from
of type SortedDictionary<TKey2, List<TValue>>
to merge them means the following. For each TKey2 key
in from
there are two possiblities:
key
is in to
. In this case add the list from[key]
to the list to[key]
.key
is not in to
. In this case add key
to to
with value from[key]
.Then, remove key fromKey
from the dictionary.
Let's translate this to code:
Given a
SortedDictionary<TKey1, SortedDictionary<TKey2, List<TValue>>>
and two keysintoKey
andfromKey
in the dictionary
SortedDictionary<TKey2, List<TValue>> to;
SortedDictionary<TKey2, List<TValue>> from;
// check that dictionary has intoKey
if (!dictionary.TryGetValue(intoKey, out to)) {
throw new ArgumentOutOfRangeException("intoKey");
}
// check that dictionary has fromKey
if (!dictionary.TryGetValue(fromKey, out from)) {
throw new ArgumentOutOfRangeException("fromKey");
}
For each
TKey2 key
infrom
there are two possiblities:
foreach(TKey2 key in from.Keys) {
// key is in to
if (to.Keys.Contains(key)) {
// add the list from[key] to the list to[key]
to[key].AddRange(from[key]);
}
// key is not in to
else {
// add an entry (key, from[key]) to the dictionary
to.Add(key, from[key]);
}
}
Then, remove key
fromKey
from the dictionary.
dictionary.Remove(fromKey);
The rest of the code is just error checking
Upvotes: 2
Reputation: 887459
LINQ will not help much here.
You should loop through each KeyValuePair in the second dictionary, call TryGetValue
to find the corresponding sub-dictionary for the key in the first dictionary (if it's not there, add it), then repeat the process for the sub-dictionary, and finally, add all of the items in the list from the second dictionary to the corresponding list in the first dictionary.
You should be able to convert that to C# relatively easily; we will not write all of your code for you.
Upvotes: 1