This is an extension method to the IEnumerable`1 interface that can select all children recursively of an entity in a tree like structure.
Example
var allChildren = node.ChildNodes.SelectRecursive(n => n.ChildNodes);
public static class IEnumerableExtensions
{
public static IEnumerable<IRecursion<T>> SelectRecursive<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> selector)
{
return SelectRecursive(source, selector, null);
}
public static IEnumerable<IRecursion<T>> SelectRecursive<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> selector, Func<IRecursion<T>, bool> predicate)
{
return SelectRecursive(source, selector, predicate, 0);
}
private static IEnumerable<IRecursion<T>> SelectRecursive<T>(IEnumerable<T> source, Func<T, IEnumerable<T>> selector, Func<IRecursion<T>, bool> predicate, int depth)
{
var q = source
.Select(item => new Recursion<T>(depth, item))
.Cast<IRecursion<T>>();
if (predicate != null)
q = q.Where(predicate);
foreach (var item in q)
{
yield return item;
foreach (var item2 in SelectRecursive(selector(item.Item), selector, predicate, depth + 1))
yield return item2;
}
}
private class Recursion<T> : IRecursion<T>
{
private int _depth;
private T _item;
public int Depth
{
get { return _depth; }
}
public T Item
{
get { return _item; }
}
public Recursion(int depth, T item)
{
_depth = depth;
_item = item;
}
}
}
public interface IRecursion<T>
{
int Depth { get; }
T Item { get; }
}