:
static public IEnumerable<T> GetAncestors<T>(this T source, Func<T, T> parentOf)
{
var Parent = parentOf(source);
while (Parent != null)
{
yield return Parent;
Parent = parentOf(Parent);
}
}
Therefore, I can use it for all hierarchies in my application:
public IEnumerable<District> Ancestors
{
get
{
return this.GetAncestors(d => d.Parent);
}
}
Or expand all the parent nodes of the given TreeView node ...
Node.GetAncestors(node => node.Parent).ToList().ForEach(n => n.Expanded = true);
It is really convenient.
source
share