Copied to clipboard

Flag this post as spam?

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


  • Francis 23 posts 123 karma points
    Sep 23, 2024 @ 18:07
    Francis
    0

    Alternative to Descendants()

    Hello Team,

    I wonder if you can help me out. I have this structure:

    enter image description here

    Now I need to be able to query ALL "pageType" under "Folder One" which means including all the "pageType" under "Sub Folder One". The only query I know of that gets all children and grand children is Descendants() so my current query is:

    folderOne.Descendants().Where(x=>x.ContentType.Alias == "pageType")
    

    However, it was said that Descendants() is not performant as per this documentation: https://docs.umbraco.com/umbraco-cms/reference/common-pitfalls#querying-with-descendants-descendantsorself

    If this is the case, is there an alternative instead of using Descendants() that could achieve the same result and is more performant?

    Thanks.

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Sep 25, 2024 @ 15:34
    Alex Skrypnyk
    0

    Hi Francis

    I think this is what you need:

    folderOne.ChildrenOfType("folderType").First().ChildrenOfType("pageType");
    
  • Francis 23 posts 123 karma points
    Sep 25, 2024 @ 16:47
    Francis
    0

    Thank for the response Alex,

    I've tested this but it only obtained all pages under "Sub Folder One". What I actually need is to obtain all the pages (pageType) under Folder One and Sub Folder One

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Sep 26, 2024 @ 16:25
    Alex Skrypnyk
    100

    Something like this than

    var list1 = folderOne.ChildrenOfType("pageType").ToList();
    var list2 = folderOne.ChildrenOfType("folderType").First().ChildrenOfType("pageType").ToList();
    
    list1.AddRange(list2);
    

    list1 should contain all needed

  • Francis 23 posts 123 karma points
    Sep 30, 2024 @ 09:07
    Francis
    0

    Hi Alex,

    Thank you for the suggestion. I will go ahead and give this a shot.

    Regards

  • Garðar Þorsteinsson 119 posts 565 karma points
    Sep 30, 2024 @ 10:47
    Garðar Þorsteinsson
    1

    Hi Francis,

    I just wanted to add that there is nothing wrong with using Descendants as long as you know what you are using it for.

    Common pitfalls around it have been over using it to select a specific node from the tree with no understanding that it will iterate over the whole tree, and if its large it can be very slow.

    As the documentations says you have to understand the implications.

    But in many cases its very handy for example if you have for a article/category tree like this:

    • Article Repository
      • Category -> Articles...
      • Category -> Articles...
      • Category -> Articles...

    Then you could use Descendants to find all the articles under the repository.

    It all depends on the size of the tree.

    In your case I see no problem with using Descendants they way you were using it.

  • Francis 23 posts 123 karma points
    Oct 03, 2024 @ 17:52
    Francis
    0

    Hi Garðar,

    Thank you. I appreciate the response. We're still doing some review actually and weighing our options but your input does help.

    Regards,

    Francis

Please Sign in or register to post replies

Write your reply to:

Draft