簡體   English   中英

C#追加字典

[英]C# appending 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());
        }

我想連接/追加字典,而無需輸入兩次。

我曾經為此寫過一個擴展方法,因為我需要同樣的方法。 最后一個參數決定遇到重復鍵時該怎么做。

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

我認為嘗試使用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;
        }
    }

我試試這個:

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

它工作正常

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM