I\'m already familiar with Linq but have little understanding of extension methods I\'m hoping someone can help me out.
So I have this hierarchical collection pseudo cod
I'm just refactoring dtb's solution to make it more generic. Try this Extension method:
public static IEnumerable Flatten(this IEnumerable source, Func recursion) where R : IEnumerable
{
return source.SelectMany(x => (recursion(x) != null && recursion(x).Any()) ? recursion(x).Flatten(recursion) : null)
.Where(x => x != null);
}
And you can use it like this:
productList.Flatten(x => x.Children).Where(x => x.ID == id);