Flatten a C# Dictionary of Lists with Linq


SUBMITTED BY: Guest

DATE: Nov. 11, 2013, 9:11 p.m.

FORMAT: Text only

SIZE: 637 Bytes

HITS: 2054

  1. Flatten a C# Dictionary of Lists with Linq
  2. Dictionary<string, List<string>>
  3. var list = dictionary.Values // To get just the List<string>s
  4. .SelectMany(x => x) // Flatten
  5. .ToList(); // Listify
  6. Dictionary.Values.SelectMany(x => x).ToList()
  7. dict.SelectMany(pair => pair.Value.Select(str => str));
  8. var flattened = from p in dictionary
  9. from s in p.Value
  10. select s;
  11. var flattened = dictionary.SelectMany(p => p.Value);
  12. dict.Values.Aggregate(new List<String>(), (a, b) => a.Concat(b));

comments powered by Disqus