John Ohara
John Ohara

Reputation: 2901

Check property value in nested list with unknown levels

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.

Upvotes: 0

Views: 523

Answers (2)

Phil
Phil

Reputation: 43001

How about something like

void Main()
{
    var nodes = new List<NestedNode>();

    var isActive = nodes.Any(n => n.AnyActive("url"));
}

public class NestedNode
{
    public NestedNode()
    {
        Children = Enumerable.Empty<NestedNode>();
    }
    public string Url { get; set; }
    public IEnumerable<NestedNode> Children { get; set; }

    public bool AnyActive(string url){ return Url==url || Children.Any(c => c.AnyActive(url));}
}

Upvotes: 1

Zohar Peled
Zohar Peled

Reputation: 82504

In this situation I would probably add a method to the NestedNode to check the condition recursively - something like this:

public bool ExistsRecursive(Func<NestedNode, bool> predicate)
{
    if(predicate(this))
    {
        return true;
    }
    foreach(var node in Children)
    {
        return predicate(node);
    }
    return false;
}

And then, in your Page_Load, all you need is this:

if(nodes.ExistsRecursive(n => n.Url == currentUrl))
{
    // current url is found in at least one node
}

Upvotes: 0

Related Questions