I have a
Lookup<TKey, TElement>
where the TElement refers to a string of words. I want to convert Lookup into:
Dictionary<int ,string []> or List<List<string>> ?
I have read some articles about using the
Lookup<TKey, TElement>
but it wasn't enough for me to understand. Thanks in advance.
A lookup is a collection of mappings from a key to a collection of values. Given a key you can get the collection of associated values:
As it implements
IEnumerable<IGrouping<TKey, TValue>>
you can use the enumerable extension methods to transform it to your desired structures:You can do that using these methods:
Enumerable.ToDictionary<TSource, TKey>
Enumerable.ToList<TSource>
Lets say you have a
Lookup<int, string>
calledmylookup
with the strings containing several words, then you can put theIGrouping
values into astring[]
and pack the whole thing into a dictionary:Update
Having read your comment, I know what you actually want to do with your lookup (see the ops previous question). You dont have to convert it into a dictionary or list. Just use the lookup directly:
Also, if the order is important, you can always sort the
IEnumerable
s via theOrderBy
orOrderByDescending
extension methods.Edit:
look at the edited code sample above: If you want to order the keys, just use the
OrderBy
method. The same way you could order the words alphabetically by usinggrouping.OrderBy(x => x)
.