I have an object called Device. A Device can have one parent Device. A Device can also have n child Devices.
I have a drop down list that shows all the selectable Devices. I can get all the Devices in the database quite easily – db.Devices.
The hierarchy can be infinite levels deep.
I need to get all Devices that aren’t above or below a given Device in the tree. Essentially I’m asking for Devices unrelated to a given Device (neither a parent/grandparent/great grandparent/etc or a child/grandchild/great grandchild/etc). I also need to exclude the given Device from the list.
What is the best way to do this? Should I use recursion?
(I am using C# and Entity Framework with an SQL Server database, so I can use Linq To SQL or use the model itself.)
My approach would be first to get all of the siblings of the device
D:Any descendants of any
d in sibsis unrelated toD. Keep going up the family tree:Continuing this way, the set
sibsand all their descendants is the set you’re after.In pseudocode: