3

这有点与这个问题有关,关于如何在 C# 中合并两个字典。提出了一个优雅的 Linq 解决方案,很酷。

但是,该问题与 相关Dictionary<Object1, Object2>,而我有一本字典,其中的值为 a Dictionary<string, Object>

我正在寻找合并的解决方案 tow 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 ...");
            }             
        }
    
4

4 回答 4

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.

于 2013-02-13T11:44:41.663 回答
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
}

这需要 aIEnumerable<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));
于 2013-02-13T13:58:36.723 回答
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 ...");
        }
    }
于 2013-02-13T13:19:10.380 回答
0

尝试这个

var 结果 = a.Concat(b).GroupBy(c => c.Key).ToDictionary>(

于 2013-02-13T11:17:24.713 回答