I have a nested menu - here a simplified class:
public class NestedNode
{
public string Url { get; set; }
public List<NestedNode> Children { get; set; }
}
Given that I have a recursive list of NestedNode
, I'm trying to ascertain whether any descendant is active at any level.
Here's the code to test:
protected void Page_Load(object sender, EventArgs e)
{
// The url of the current page
var currentUrl = Request.Url.GetLeftPart(UriPartial.Path);
// This is a list of nested nodes
var nodes = SiloNodes;
// Start loop
RecursiveCall(nodes, currentUrl);
}
void RecursiveCall(IEnumerable<NestedNode> nodes, string currentUrl)
{
if (nodes == null) return;
foreach (var n in nodes)
{
// This can test current level only
//var isActive = n.Url == currentUrl;
// This can test next level down
//var isActive = n.Children.Any(c => c.Url == currentUrl);
// How can I test all levels in one go?
RecursiveCall(n.Children, currentUrl);
}
}
What I need to be able to do is work out if any of the parents children are active (at the top lavel) so that I can add classes. At the moment, my ideas only go one level deep.
RecursiveCall
is not performing as expected on the child lists below the first level?