'Search for ancestors in a hierarchical list

Created a method to search for children in a hierarchical list. Is there a way to do the method over the stack?

public static IEnumerable<T> GetSelfAndAncestors<T>(this T item, Func<T, T> parentSelector)
{
    var parent = parentSelector(item);
    if (parent==null)
        return item.ToIEnumerable();

    return item.ToIEnumerable().Concat(parent.GetSelfAndAncestors(parentSelector));
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source