【问题标题】:C# appending DictionaryC# 附加字典
【发布时间】:2012-10-06 17:24:55
【问题描述】:

如何将 Dictionary 添加到另一个 Dictionary 中?

Dictionary<string, DateTime> odict = new Dictionary<string, DateTime>();
        //Liste tous les Clients EMAIL (3)
        foreach (var TheClientID in TheClient.GetClient_Id(3))
        {
            Dictionary<string, DateTime> Temp = oEmail.VoirEML(TheClientID.Key);
             //odict = odict.Concat(Temp).ToDictionary(pair => pair.Key, pair => pair.Value);
            odict = odict.SelectMany(toto => Temp)
                     .ToLookup(pair => pair.Key, pair => pair.Value)
                     .ToDictionary(group => group.Key, group => group.First());
        }

我想连接/附加字典,而不是双倍的条目。

【问题讨论】:

标签: c# dictionary append concat


【解决方案1】:

我曾经为此编写了一个扩展方法,因为我需要同样的方法。最后一个参数决定遇到重复键时的处理方式。

Skip 表示保留源条目。

Overwrite 表示使用“其他”条目。

Throw 会抛出异常。

public enum DuplicateKeyHandling
{
    Skip,
    Overwrite,
    Throw
}

/// <summary>
/// Combines two dictionaries into one dictionary. The contents of the resulting dictionary depends on the <paramref name="duplicateKeyHandling"/> parameter.
/// </summary>
/// <typeparam name="TKey">The type of the keys in the dictionaries</typeparam>
/// <typeparam name="TValue">The type of the values in the dictionaries</typeparam>
/// <param name="this"></param>
/// <param name="other">The dictionary to combine this one with</param>
/// <param name="duplicateKeyHandling">Specifies how to react if the <paramref name="other"/> dictionary contains keys that are already in this dictionary</param>
/// <returns>A new dictionary containing the combined key-value-pairs of both source dictionaries</returns>
public static Dictionary<TKey, TValue> Combine<TKey, TValue>(this Dictionary<TKey, TValue> @this, Dictionary<TKey, TValue> other, DuplicateKeyHandling duplicateKeyHandling = DuplicateKeyHandling.Skip)
{
    // EDIT: I added the "comparer" parameter so the resulting dictionary will
    // use the same comparer as the first source dictionary
    var result = new Dictionary<TKey, TValue>(@this, @this.Comparer);

    foreach (KeyValuePair<TKey, TValue> kvp in other)
    {
        if (result.ContainsKey(kvp.Key))
        {
            if (duplicateKeyHandling == DuplicateKeyHandling.Overwrite)
            {
                result[kvp.Key] = kvp.Value;
            }

            if (duplicateKeyHandling == DuplicateKeyHandling.Throw)
            {
                throw new Exception("Duplicate key while combining dictionaries!");
            }
        }
        else
        {
            result.Add(kvp.Key, kvp.Value);
        }
    }

    return result;
}

【讨论】:

    【解决方案2】:

    我认为尝试使用 Linq 是一个错误。使用简单的循环即可轻松编写和阅读,扩展方法的效率不会更高——可能效率更低。

        Dictionary<string, DateTime> odict = new Dictionary<string, DateTime>();
        //Liste tous les Clients EMAIL (3)
        foreach(var TheClientID in TheClient.GetClient_Id(3)) {
                Dictionary<string, DateTime> Temp = oEmail.VoirEML(TheClientID.Key);
                //odict = odict.Concat(Temp).ToDictionary(pair => pair.Key, pair => pair.Value);
                foreach(var kvp in Temp){
                        odict[kvp.Key] = kvp.Value;
            }
        }
    

    【讨论】:

      【解决方案3】:

      我试试这个:

      odict = odict.Concat(Temp.Where(kvp => !odict.ContainsKey(kvp.Key))).ToDictionary(x => x.Key, x => x.Value);
      

      效果很好

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-24
        • 1970-01-01
        • 1970-01-01
        • 2012-02-14
        • 2017-12-28
        • 2015-06-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多