C#追加字典

3
如何将一个字典附加到另一个字典?
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());
        }

我想将字典连接/追加,但不要重复条目。

如果有重复的键,你想如何处理? - James Michael Hare
目前你想做什么还不是很清楚... 你能给出具体的例子吗? - Jon Skeet
1
重复问题:https://dev59.com/I3VC5IYBdhLWcg3wcgqd - Mike Corcoran
3个回答

8

我曾经编写了一个扩展方法,因为我需要相同的功能。最后一个参数决定了在遇到重复键时该怎么做。

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

0

我认为尝试使用 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;
        }
    }

0
我尝试了这个:
odict = odict.Concat(Temp.Where(kvp => !odict.ContainsKey(kvp.Key))).ToDictionary(x => x.Key, x => x.Value);

它运行良好


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接