Unlike Dictionary
, you cannot construct a Lookup
by adding elements one by one. Do you happen to know the reason?
Lookup
is just like multimap
in C++; why can't we modify it in C#? If we really can't, how can we construct a multimap
data structure in C#?
Lookup
andILookup
were introduced as part of LINQ, which generally takes a more functional approach than other aspects of the framework. Personally I like the fact thatLookup
is (at least publicly) immutable - and I'm looking forward to more immutable collections being available.If you want to create your own multimap data structure, just maintain a
Dictionary<TKey, List<TValue>>
or something similar. You might want to look at my Edulinq implementation ofLookup
for some sample code.I had this same problem and question. Why is Lookup immutable? I solved it with some extension methods to IDictionary
Here is an implementation I wrote
and a test case ( probably not exhaustive ) for you