Nathan Baulch

Thursday, October 6, 2016

Generic extension method for traversing object graphs

  1. public static IEnumerable Traverse(this T root, Func> getChildren)
  2. {
  3. return Enumerable.Repeat(root, 1)
  4. .Concat((getChildren(root) ?? Enumerable.Empty())
  5. .SelectMany(child => Traverse(child, getChildren)));
  6. }
Posted by Nathan Baulch at 10:17 PM No comments:
Labels: LINQ
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Blog Archive

  • ►  2024 (1)
    • ►  October (1)
  • ►  2022 (1)
    • ►  February (1)
  • ►  2019 (1)
    • ►  December (1)
  • ▼  2016 (2)
    • ▼  October (1)
      • Generic extension method for traversing object graphs
    • ►  August (1)
  • ►  2014 (1)
    • ►  April (1)
  • ►  2013 (4)
    • ►  November (1)
    • ►  March (1)
    • ►  January (2)
  • ►  2012 (7)
    • ►  June (1)
    • ►  February (3)
    • ►  January (3)
  • ►  2011 (4)
    • ►  December (4)

Stack Overflow

Profile for Nathan Baulch at Stack Overflow, Q&A for professional and enthusiast programmers

About Me

Nathan Baulch
View my complete profile
Simple theme. Powered by Blogger.