Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michael Sims 119 posts 387 karma points
    Jun 15, 2012 @ 15:14
    Michael Sims
    0

    Get descendants where nodeTypeAlias in array

    I am trying to get the descendants of a node where the nodeTypeAlias exists as an entry in a string array that may have a variable number of entries.

    Does anyone have any ideas about how to do this?

    Thanks,
    Mike

  • Douglas Ludlow 210 posts 366 karma points
    Jun 15, 2012 @ 17:36
    Douglas Ludlow
    0

    I got the following to work. I put the nodes and aliases into Lists so I could use the IEnumerable methods, Where and Any. Just set the root to whatever node you want.

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @try
    {
    dynamic root = Library.NodeById(-1);
    DynamicNodeList list = root.Descendants();

    List<DynamicNode> descendants = new List<DynamicNode>(list.Cast<DynamicNode>());
    List<string> aliases = new List<string> {"umbHomepage", "umbTextpage"};

    var filtered = descendants.Where(n => aliases.Any(a => a == n.NodeTypeAlias));
    <ul>
    @foreach (var node in filtered)
    {
    <li>@node.Name</li>
    }
    </ul>
    }
    catch (Exception e)
    {
    @e.ToString()
    }

     

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jun 18, 2012 @ 13:42
    Jamie Pollock
    0

    Hi Douglas,
    Personally I'd use the following to populate filtered:

    var filtered = descendants.Where(n => aliases.Contains(n.NodeTypeAlias));

    To me, it looks a little cleaner, but your answer is completely valid.

    Jamie



  • René Andersen 238 posts 684 karma points
    Apr 11, 2013 @ 10:19
    René Andersen
    0
Please Sign in or register to post replies

Write your reply to:

Draft