Created
January 18, 2012 13:53
-
-
Save mfloryan/1633091 to your computer and use it in GitHub Desktop.
Enumerate a tree deep
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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