i have a data structure like this
public class Employee
{
public string Name { get; set; }
public IEnumerable<Employee> Employees { get; private set; }
// ...
}
Now i need to loop through the complete structure and execute a method on each item.
How can i create an extension on IEnumerable for such a traverse function.
Wonderfull would be something like this
employeList.Traverse(e => Save(e), e.Employees.Count > 0);
Or is it impossible and i have to create a special method in my business logic?
Thanks a lot.
Do you mean an extension method on
IEnumerable<Employee>? That’s certainly feasible:This has to be in a static, non-generic, non-nested class.
I’m not sure what the predicate bit is for, mind you…
EDIT: Here’s the more generalised form I think you were looking for:
You’d then call it with: