Is there any way I can get a chain of entities from EF that represents a chain from a Child all the way up to the root Parent entity in a self-referencing hierarchy? I could of course do this with a while loop, such as:
var chain = new List<Node>();
var thisNode = db.Nodes.First(n => n.Id == [whatever]);
do {
chain.Add(thisNode);
thisNode = thisNode.ParentId == null ? null : thisNode.Parent;
} while (thisNode != null);
But I think this would result in one query to the DB for each level in the hierarchy. While we're talking about an admittedly small performance gain here, is there a way I can express this logic as a single LINQ query to get the whole chain of companies from a given child all the way up to a parent where the parent meets a particular condition? Or do I need to use a loop like this?