So the above code is a parent-child walking hierarchy in the up (parent) direction. Therefore, in the worst case, this will lead to n queries being loaded into the database for the hierarchy depth n. I suggest you try deferred execution by slightly changing the method, e.g.
public IEnumerable<Node> GetAllParents(IEnumerable<Node> records)
{
if (this.ParentID == 0)
{
return new List<Node>();
}
var parent = records.Where(p => p.ID == ParentID);
var parents = parent.Concat(parent.GetAllParents(records));
return parent;
}
100%, , , / , .
, proc/view, ( CTE sql- ).
: Where First , - (: )