Copied to clipboard

Flag this post as spam?

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


  • Sri Harsha 8 posts 38 karma points
    Nov 06, 2013 @ 03:16
    Sri Harsha
    0

    Filtering a node list using WHERE

    Hi,

    I'm having issues filtering a node list using Where. 

    Problem: 

    I have nodes called categories and sub-categories. The sub-categories has a field called 'masterCategory' which is a content picker that lists all categories. When a user clicks on a Category, I take that nodeId and then compare it to the masterCategory property of each sub-category. This is the current code and it works fine. Except, it will create a div with class 'row' for every sub-category even when there is no match.

    @* Fetch the model for the Sub Categories folder *@
            dynamic subCategoryNode = Model.NodeById(1378);
    
            @* Fetch the ID for the currently selected Category *@
            var nodeId = Model.Id.ToString();                       
    
            @* Loop through all children of the subCategory folder - Group every 3 children together *@
            foreach(var group in subCategoryNode.Descendants().InGroupsOf(3)) {                     
    
                @* Create a new row for every fourth child *@
                <div class="row">
    
                    @* Loop through the 3 children group created above *@
                    @foreach(var item in group) {
    
                        @* Get the ID of the current subCategory *@
                        var subNodeId = item.masterCategory.ToString();
    
                        @* Compare the subCategory ID with the Category ID, then add it to the page if the ID's match *@
                        if(nodeId == subNodeId) {

    What I want to do is filter the nodes in the foreach using 'Where' instead of using an If statement. Something like below

    foreach(var group in subCategoryNode.Descendants().Where("nodeId == masterCategory").InGroupsOf(3))

    This currently doesn't work. The macro doesn't give any errors but it doesn't return anything in the group either.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 06, 2013 @ 08:22
    Jeavon Leopold
    102

    Hi Sri,

    Try

    foreach(var group in subCategoryNode.Descendants().Where("masterCategory == @0", nodeId).InGroupsOf(3))
    

    Jeavon

  • Sri Harsha 8 posts 38 karma points
    Nov 06, 2013 @ 10:28
    Sri Harsha
    0

    Thanks Jeavon, that's awesome. In a slightly related note, is there a way to manually construct a node list with select nodes?

    In the above example, sub-categories is a folder with several child nodes. I can access its children with @Model.Children but the only way I can work with them now is using a foreach. What if I want to construct a nodelist with child nodes that match a specific criteria, so I can use this node list later.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 06, 2013 @ 10:47
    Jeavon Leopold
    1

    Great! Yes, you can, something like this:

    var nodeList = new List<int>(){1234, 3456, 3432};    
    IEnumerable<DynamicNode> publishedNodeList = Library.NodesById(nodeList);   
    
  • Funka! 398 posts 661 karma points
    Nov 07, 2013 @ 23:55
    Funka!
    1

    Jeavon's last post suggesting NodesById is helpful and concise if you are working with the ID numbers and can build them up like this, but I've found often you want to actually reference a Node (not just the ID) and check certain properties on it before you know whether or not it should exist in your list. And if you already have these nodes (for example, as you're iterating through Model.Children), you can just add them to a list as you go and not have to load/instantiate them twice. For example,

    var list = new List<DynamicNode>();
    foreach (var child in Model.Children.Where("Visible"))
    {
        // check properties on node, whether it is visible or whatever...
        if (child.magicPropertySaysYes)
        {
            list.Add(child);
        }
    }
    

    Good luck!

  • Sri Harsha 8 posts 38 karma points
    Nov 08, 2013 @ 00:02
    Sri Harsha
    0

    Thanks Funka!. I have seen this syntax (<something>) used in a lot of threads but whenever I try to add it to a cshtml file in umbraco, it always throws an error. I haven't tried the code you added above. I'll give it a go now.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 08, 2013 @ 08:05
    Jeavon Leopold
    0

    Funka's example looks great, give it a try!

Please Sign in or register to post replies

Write your reply to:

Draft