C# 附加字典 [英] C# appending Dictionary

查看:20
本文介绍了C# 附加字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

how can i append Dictionary into another 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());
        }

我想在不加倍条目的情况下连接/附加字典.

i want to concat / append dictionary without double the entry.

推荐答案

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

I once wrote an extension method for that because I needed the same. The last parameter decides what to do when duplicate keys are encountered.

Skip 表示保留源条目.

Overwrite 表示使用其他"条目.

Overwrite means "other" entry is used.

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;
}

这篇关于C# 附加字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆