Do you mean the extension method on IEnumerable<Employee> ? This is certainly possible:
public static void Traverse(this IEnumerable<Employee> employees, Action<Employee> action, Func<Employee, bool> predicate) { foreach (Employee employee in employees) { action(employee);
This should be in a static, not general, not nested class.
I'm not sure what the predicate bit is for, mind you ...
EDIT: Here's a more generalized form, I think you were looking for:
public static void Traverse<T>(this IEnumerable<T> items, Action<T> action, Func<T, IEnumerable<T>> childrenProvider) { foreach (T item in items) { action(item); Traverse<T>(childrenProvider(item), action, childrenProvider); } }
Then you call it with:
employees.Traverse(e => Save(e), e => e.Employees);
source share