A dictionary with multiple entries with the same key [duplicate]

In .NET 3.5 you can use a Lookup instead of a Dictionary.

var items = new List<KeyValuePair<int, String>>();
items.Add(new KeyValuePair<int, String>(1, "first"));
items.Add(new KeyValuePair<int, String>(1, "second"));
var lookup = items.ToLookup(kvp => kvp.Key, kvp => kvp.Value);

foreach (string x in lookup[1])
{
    Console.WriteLine(x);
}

The Lookup class is immutable. If you want a mutable version you can use EditableLookup from MiscUtil.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)