I have an entity called Category and the entity contains a IEnumerable called ChildCategories. A category can have these child categories which can have it's own child categories and so on.
Say I have selected out the top level parent category, I want to get all child categories and their child categories and so on so that I have all hierarchical children of the category. I want this flatterned and returned with the initial category. I've tried creating something like
public static IEnumerable<T> AllChildren<T>(this IEnumerable<T> items,
Func<T, IEnumerable<T>> children, bool includeSelf)
{
foreach (var item in items)
{
if (includeSelf)
{
yield return item;
}
if (children != null)
{
foreach (var a in children(item))
{
yield return a;
children(a).AllChildren(children, false);
}
}
}
}
Which would get flatterned after using SelectMany method but havn't quite got it.