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.
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'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);
}
}
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.
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.
What I want to do is filter the nodes in the foreach using 'Where' instead of using an If statement. Something like below
This currently doesn't work. The macro doesn't give any errors but it doesn't return anything in the group either.
Hi Sri,
Try
Jeavon
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.
Great! Yes, you can, something like this:
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,Good luck!
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.
Funka's example looks great, give it a try!
is working on a reply...