Skip to content

Instantly share code, notes, and snippets.

@mfloryan
Created January 18, 2012 13:53
Show Gist options
  • Select an option

  • Save mfloryan/1633091 to your computer and use it in GitHub Desktop.

Select an option

Save mfloryan/1633091 to your computer and use it in GitHub Desktop.
Enumerate a tree deep
public static class EnumerableExtensions
{
public static IEnumerable<T> SelectDeep<T>(
this IEnumerable<T> source, Func<T, IEnumerable<T>> selector)
{
if (source == null) yield break;
foreach (T item in source)
{
yield return item;
foreach (T subItem in SelectDeep(selector(item), selector))
{
yield return subItem;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment