So I have a tree structure in a SQL-Server database. Each node is linked to its parent by a foreign key called prev, to another node.
I would like whenever I fetch a node, for all nodes leading to the root of the tree to be fetched as well. Currently I can do it like this:
MyDataContext db = new MyDataContext();
IList<Node> nodes = new List<Node>();
Node node = db.Nodes.Single(x => x.id == 14);
nodes.Add(node);
while (node.prev != null)
{
node = db.Nodes.Single(x => x.id == node.prev);
nodes.Add(node);
}
but this will require a number of queries equal to the depth of my tree. I am a little new to linq-to-sql, so I'm not sure how to tell it to fetch recursively like that. Is it possible?