合并两个词典Dictionary<;字符串,字典<;字符串,对象>>;
本文关键字:字符串 lt gt 字典 对象 Dictionary 两个 合并 | 更新日期: 2023-09-27 17:58:52
这与如何在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 ..."); } }
我不太清楚你想要什么。这试图合并两个字典:
// 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
。
尝试这个
var result=a.Concat(b).GroupBy(c=>c.Key).ToDictionary>(
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 ...");
}
}
您应该问自己的第一个问题:合并两个字典的结果是什么类型的?如果值共享同一个键,如何将它们合并在一起?
可能是这样的:
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));