Traverse nested lists (IEnumerable)


/ Published in: C#
Save to your folder(s)

Credit: Tim Schmelter


Copy this code and paste it in your HTML
  1. public static IEnumerable<T> Traverse<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> fnRecurse)
  2. {
  3. foreach (T item in source)
  4. {
  5. yield return item;
  6.  
  7. IEnumerable<T> seqRecurse = fnRecurse(item);
  8. if (seqRecurse != null)
  9. {
  10. foreach (T itemRecurse in Traverse(seqRecurse, fnRecurse))
  11. {
  12. yield return itemRecurse;
  13. }
  14. }
  15. }
  16. }

URL: http://stackoverflow.com/questions/12318372/linq-nested-list-contains

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.