I have SortedDictionary<int, Dictionary<string, HashSet<long>>>
How can I flatten the result using Linq?
I've tried:
var result = dictionary.Values.SelectMany(x => x);
but how do I flatten to the very bottom?
I need to get a collection of int, string, HashSet<long>
CodePudding user response:
You may use something like the following:
var flattened = dictionary.SelectMany(x => x.Value.Select(y => (x.Key, y.Key, y.Value)));
This will produce an IEnumerable of ValueTyple<int, string, HashSet<long>>
.
Note that because the type here is IEnumerable
, it's not yet materialized. You might want to add a .ToList()
to the end (in which case, the type will be List<ValueTyple<int, string, HashSet<long>>>
) or convert it to any other collection type you desire.