这与此问题有关,关于如何在C#中合并两个词典。提出了一个优雅的Linq解决方案,很酷。
但是,这个问题与Dictionary<Object1, Object2>
有关,而我有一个字典,其值为Dictionary<string, Object>
。
我正在寻找合并拖曳Dictionary<string, Dictionary<string, Object>>
的解决方案,具有以下要求:
对于每个字典,我认为KEY的分组可以是解决方案的一部分,但是......
之后internal static Dictionary<string, Dictionary<string, object>> OperationDic(Dictionary<string, Dictionary<string, object>> a, Dictionary<string, Dictionary<string, object>> b, string operation)`
{
switch (operation)
{
case "+":
var result = a.Concat(b).GroupBy(d => d.Key).ToDictionary (d => d.Key, d => d.First().Value);
return result;
default:
throw new Exception("Fail ...");
}
}
答案 0 :(得分:1)
我不太清楚你想要什么。这会尝试合并两个词典:
// first copy everything from a
var result = new Dictionary<string, Dictionary<string, object>>(a);
// now check to see if we can add stuff from b
foreach (var entryOuter in b)
{
Dictionary<string, object> existingValue;
if (result.TryGetValue(entryOuter.Key, out existingValue))
{
// there's already an entry, see if we can add to it
foreach (var entryInner in entryOuter.Value)
{
if (existingValue.ContainsKey(entryInner.Key))
throw new Exception("How can I merge two objects? Giving up.");
existingValue.Add(entryInner.Key, entryInner.Value);
}
}
else
{
// new entry
result.Add(entryOuter.Key, entryOuter.Value);
}
}
return result;
您可能想要为null
添加检查。 a
,b
和existingValue
(如果存在)可能是null
。
答案 1 :(得分:0)
试试这个
var result = a.Concat(b).GroupBy(c =&gt; c.Key).ToDictionary&gt;(
答案 2 :(得分:0)
internal static Dictionary<string, Dictionary<string, object>> OperationDic(Dictionary<string, Dictionary<string, object>> a, Dictionary<string, Dictionary<string, object>> b, string operation)
{
var result = new Dictionary<string, Dictionary<string, object>>(a);
switch (operation)
{
case "+":
// now check to see if we can add stuff from b
foreach (var entryOuter in b)
{
Dictionary<string, object> existingValue;
if (result.TryGetValue(entryOuter.Key, out existingValue))
{
Dictionary<string, object> Value = entryOuter.Value;
result[entryOuter.Key] = existingValue.Concat(Value).GroupBy(d => d.Key).ToDictionary(d => d.Key, d => d.First().Value);
}
else
{
// new entry
result.Add(entryOuter.Key, entryOuter.Value);
}
}
return result;
default:
throw new Exception("FAIL ...");
}
}
答案 3 :(得分:0)
您应该问自己的第一个问题:合并两个词典的结果有哪些类型?如果值共享相同的密钥,如何将值合并在一起?
可能是这样的:
public static Dictionary<TKey, IEnumerable<TValue>> Merge<TKey, TValue>(this IDictionary<TKey, TValue> this_, IDictionary<TKey, TValue> other)
{
return this_.Concat(other). // IEnumerable<KeyValuePair<TKey, TValue>>
GroupBy(kvp => kvp.Key). // grouped by the keys
ToDictionary(grp => grp.Key, grp => grp.Select(kvp => kvp.Value).Distinct());
}
因此,您的Dictionary<string, Dictionary<string, object>>
类型的两个词典将转到Dictionary<string, IEnumerable<Dictionary<string, object>>>
。
但是,您的值是字典,因此您可能希望合并它们:
public static Dictionary<TKey, IEnumerable<TValue>> Flatten<TKey, TValue>(IEnumerable<Dictionary<TKey, TValue>> dictionaries)
{
return dictionaries.SelectMany(d => d.Keys).Distinct(). // IEnumerable<TKey> containing all the keys
ToDictionary(key => key,
key => dictionaries.Where(d => d.ContainsKey(key)). // find Dictionaries that contain the key
Select(d => d.First(kvp => kvp.Key.Equals(key))). // select that key (KeyValuePair<TKey, TValue>)
Select(kvp => kvp.Value)); // and the value
}
这需要IEnumerable<Dictionary<string, object>>
并将其转换为Dictionary<string, IEnumerable<object>>
。您现在可以为Merge
生成的字典的每个值调用此方法。
电话会是:
Dictionary<string, IEnumerable<Dictionary<string, object>>> result1 = dic1.Merge(dic2);
Dictionary<string, Dictionary<string, IEnumerable<object>>> result2 = dic1.Merge(dic2).ToDictionary(kvp => kvp.Key, kvp => Flatten(kvp.Value));