Skip to content

Instantly share code, notes, and snippets.

@sergeyt
Created November 6, 2013 02:20
Show Gist options
  • Save sergeyt/7329871 to your computer and use it in GitHub Desktop.
Save sergeyt/7329871 to your computer and use it in GitHub Desktop.
flatten enumerable extension
internal static class Extensions
{
public static IEnumerable<T> Flatten<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> childSelector)
{
foreach (var item in source)
{
yield return item;
foreach (var d in childSelector(item).Flatten(childSelector))
{
yield return d;
}
}
}
public static IEnumerable<T> Flatten<T>(this T source, Func<T, IEnumerable<T>> childSelector)
{
yield return source;
foreach (var d in childSelector(source).Flatten(childSelector))
{
yield return d;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment