这不是一个LINQ问题。这是一个通用的版本,可以解决您的问题。
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);
}
}
用法:
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);
这里是你如何处理的问题是这样的。首先,指定你想要做的事情。
给定一个SortedDictionary<TKey1, SortedDictionary<TKey2, List<TValue>>>
和两个键intoKey
和fromKey
在字典中,合并与关键fromKey
字典与关键intoKey
字典。
现在指定合并两个字典的含义。给定两个字典to
和from
类型SortedDictionary<TKey2, List<TValue>>
合并它们意味着以下内容。对于from
各TKey2 key
有两个possiblities:
key
是to
。在这种情况下,将列表from[key]
添加到列表to[key]
。
key
不在to
。在这种情况下,将key
加到to
的值为from[key]
。
然后,从字典中删除键fromKey
。
让我们把这种代码:
给定一个SortedDictionary<TKey1, SortedDictionary<TKey2, List<TValue>>>
和两个键intoKey
和fromKey
在字典
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");
}
对于from
各TKey2 key
有两个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]);
}
}
然后,从字典中删除键fromKey
。
dictionary.Remove(fromKey);
代码的剩下的只是错误检查
基于粗略的检查,我说有你使用了错误的数据结构,你的工作非常好的变化。首先,虽然您有“1000”,{a,b,c,z}作为您的目标值,但您没有说明目标键应该是什么。此外,将每个值与每个值配对需要线性搜索值集合中的每个项目,使其至少为O(n^2)。有机会,你能描述一下你的问题是什么以及你的字典代表什么样的数据?从那里,有人可能会提出一个更好的数据结构来支持高效的合并。 – Juliet 2009-12-22 00:13:24
这不会是一个线性搜索。 (这是一个_Sorted_Dictionary) – SLaks 2009-12-22 00:14:44
当我看到嵌套字典时,我感到可疑。你有没有想过使用一个组合键,如'SortedDictionary,List >'?你在用什么?不同的数据结构可能更合适。 –
jason
2009-12-22 00:49:40