Posted By

RhinoX64 on 07/30/14


Tagged

lists nested IEnumerable traverse c#


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Beppoi
jhcruzr


Traverse nested lists (IEnumerable)


 / Published in: C#
 

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

Credit: Tim Schmelter

  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. }

Report this snippet  

You need to login to post a comment.