Following up on Bo's suggestion, uComponents/uQuery is ideal for this type of query. Instead of doing the LINQ Where clause, you can do the check directly in the XPath:
var nodes = uQuery.GetNodesByXPath("descendant::*[@isDoc and @level = 4 and propertyAlias = 1]");
Keep in mind this will only work published content nodes. If you needed to find unpublished content you will need to use the Document API. uQuery also has some helper methods for Document objects, you would need to reference the "uComponents.Core.uQueryExtensions" namespace and get hold of a Document object (e.g. the Homepage node/document) ... then you could do something like this:
var homepage = new Document(1060);
var level4docs = homepage.GetDescendantDocuments(n => n.Level == 4);
Efficient way to get nodes X level deep
Hi everybody.
I have a 4 levels deep node structure, where the top most level is made of 1 root node.
What I want to do is get all nodes in the 4th level for which a certain property(ies) is true, for example:
get all 4th level nodes where nodePropertyX == true.
Now, I could do this with a for-each loop, and iterate all the items in the levels above, but I have the feeling it would be inefficient.
1. How can I do it in a better way ? (maybe with a dictionary).
2. What is the preferrable method: using C# control (.ascx) or razor script (.cshtml)?
Hi olst,
Do you, by chance, have uComponents installed? If so, you should be able to do some simple XPath to get the nodes. Untested, but maybe something like:
var nodes = uQuery.GetNodesByXPath("./* [@level = 4]").Where(x => x.GetProperty("YourProperty").Value.Equals("1"));
All the best,
Bo
Following up on Bo's suggestion, uComponents/uQuery is ideal for this type of query. Instead of doing the LINQ Where clause, you can do the check directly in the XPath:
Keep in mind this will only work published content nodes. If you needed to find unpublished content you will need to use the Document API. uQuery also has some helper methods for Document objects, you would need to reference the "uComponents.Core.uQueryExtensions" namespace and get hold of a Document object (e.g. the Homepage node/document) ... then you could do something like this:
Cheers, Lee.
Of course! Thanks Lee :-) Would make sense to check for the property in the xpath string.
is working on a reply...