Use a Lookup
. This is just like a dictionary, only it allows the same key multiple times.
Here are the docs. You have to use the .ToLookup
extension method on your sequence to create one.
In your case, it seems like you'd need a ILookup<string, IList<string>>
(or int
instead of string
, i don't know your data).
Here's how you can generate the lookup:
IEnumerable<KeyValuePair<string, IEnumerable<string>> myData = new[] {
new KeyValuePair<string, IEnumerable<string>>("2", new[] { "2", "3" }),
new KeyValuePair<string, IEnumerable<string>>("2", new[] { "4", "6" }),
new KeyValuePair<string, IEnumerable<string>>("4", new[] { "3", "5", "6" }),
};
var myLookup = myData.ToLookup(item => item.Key, item => item.Value.ToList());