Home > Net >  Flatten Lookup<TKey,TValue> to key-value pairs
Flatten Lookup<TKey,TValue> to key-value pairs

Time:07-14

lookup.SelectMany(x => x) only returns values. Is there a way to retain keys associated with them?

CodePudding user response:

Lookup<TKey, TValue> implements IEnumerable<IGrouping<TKey,TElement>> and IGrouping<TKey,TElement> exposes Key property so just use it:

var listOfValueTuples = lookup
    .SelectMany(g => g.Select(v => (g.Key, v)))
    .ToList();
  • Related