Copied to clipboard

Flag this post as spam?

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


  • olst 69 posts 88 karma points
    Dec 01, 2011 @ 15:46
    olst
    0

    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)?

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Dec 01, 2011 @ 21:22
    Bo Damgaard Mortensen
    2

    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

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Dec 01, 2011 @ 23:29
    Lee Kelleher
    1

    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);

    Cheers, Lee.

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Dec 01, 2011 @ 23:39
    Bo Damgaard Mortensen
    0

    Of course! Thanks Lee :-) Would make sense to check for the property in the xpath string.

Please Sign in or register to post replies

Write your reply to:

Draft