2

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

エントリを二重にせずに辞書を連結/追加したい。

4

3 に答える 3

7

同じことが必要だったので、そのための拡張メソッドを書いたことがあります。最後のパラメーターは、重複キーが検出された場合の対処方法を決定します。

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;
}
于 2012-10-16T15:02:50.140 に答える
0

私はこれを試します:

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

そしてそれはうまくいきます

于 2012-10-17T09:25:12.400 に答える
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;
        }
    }
于 2012-10-16T19:10:45.607 に答える