Converting Lookup into other data structures c#

后端 未结 2 1757
别那么骄傲
别那么骄傲 2021-01-04 10:27

I have a

Lookup

where the TElement refers to a string of words. I want to convert Lookup into:



        
2条回答
  •  太阳男子
    2021-01-04 11:05

    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:

    TKey key;
    Lookup lookup;
    IEnumerable values = lookup[key];
    

    As it implements IEnumerable> you can use the enumerable extension methods to transform it to your desired structures:

    Lookup lookup = //whatever
    Dictionary dict = lookup.ToDictionary(grp => grp.Key, grp => grp.ToArray());
    List> lists = lookup.Select(grp => grp.ToList()).ToList();
    

提交回复
热议问题