LINQ Convert Dictionary to Lookup

前端 未结 4 1770
陌清茗
陌清茗 2020-12-16 11:57

I have a variable of type Dictionary>
I want to convert it to a Lookup.

<
4条回答
  •  不知归路
    2020-12-16 12:59

    How about:

    var lookup = dictionary.SelectMany(pair => pair.Value,
                                       (pair, Value) => new { pair.Key, Value })
                           .ToLookup(pair => pair.Key, pair => pair.Value);
    

    It does feel like a little bit of a waste doing this when the dictionary already has all the information grouped appropriately, but I can't see a simple way round that. Of course you could implement ILookup yourself with a wrapper around the dictionary...

提交回复
热议问题