Suppose that I have an enumerable on which I have applied .GroupBy()
twice to end up with nested IGroupings
:
IEnumerable<IGrouping<TOuterKey, IGrouping<TInnerKey, TElement>>> source = enumerable.GroupBy(o => o.InnerKey).GroupBy(g => g.OuterKey);
For nested IEnumerables
, it is easy to just apply .SelectMany()
to get a flattened IEnumerable
with all of the results. For IGroupings
, however, this is not so simple. Suppose I wanted a generalised extension method to do this:
public static IEnumerable<IGrouping<TResultKey, TResultElement>> SelectMany<TOuterKey, TInnerKey, TInputElement, TResultKey, TResultElement>(
this IEnumerable<IGrouping<TOuterKey, IGrouping<TInnerKey, TInputElement>>> source,
Func<IGrouping<TOuterKey, IGrouping<TInnerKey, TInputElement>>, TResultKey> keySelector,
Func<TOuterKey, TInnerKey, TInputElement, TResultElement> valueSelector)
How would I create that?