Copied to clipboard

Flag this post as spam?

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


  • Niels Hartvig 1951 posts 2391 karma points c-trib
    Aug 22, 2019 @ 09:47
    Niels Hartvig
    0

    How do you prefer to query data in Umbraco 8?

    Given a medium sized site (2.000-5.000 content nodes), I need the most efficient way to find the published versions of

    1. All content of a specific document type within a ContentRoot
    2. The first occurrance of a specific document type within a ContentRoot

    How would you solve those two? Ie. how would your query in your view look like?

    Best,

    Niels...

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Aug 22, 2019 @ 10:10
    Nik
    0

    For part 1, I'd look to use Examine to find all the nodes of a specific type.

    So something like:

     var query = searcher.CreateQuery().NodeTypeAlias(MyContentType.ModelTypeAlias);
    

    But this is on the assumption that by ContentRoot you mean the very root of the entire content section.

    With regards to query 2, what do you mean by first occurrence? In this example is Node 2 or Node 3 the first occurrence?

    Root
       > Node 1 (doc type A)
                > Node 2 (doc type B)
       > Node 3 (doc type B)
    

    Thanks

    Nik

  • Niels Hartvig 1951 posts 2391 karma points c-trib
    Aug 22, 2019 @ 10:56
    Niels Hartvig
    0

    Wonderful - keep it coming. As for the scenarios, I should have been more clear. By ContentRoot, I didn't meant the "Root" but the "Site" ie. Node 1 or Node 3.

    For query 2, assuming that I was somewhere deep in the "node 1" part of the structure, I'd want to find Node 2 (or "first occurrence in the site of something with a doctype B).

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Aug 22, 2019 @ 15:47
    Nik
    0

    Okay, so if I had a site structure as follows:

    Root
     > site root
       > Page 1 (A)
         > Page 1.1 (B)
            > Page 1.1.1 (D)
               > Page 1.1.1.1 (E)
         > Page 1.2 (B)
         > Page 1.3 (C)
         > Page 1.4 (D)
       > Page 2 (D)
          > Page 2.1 (B)
          > Page 2.2 (C)
          > Page 2.3 (D)
    

    And say I was on Page 1.1.1.1

    Then,

    If I wanted to find the first occurance of Node type D Assuming that the first occurrence in this instance means working down each tree branch until the first occurrence is found then I'd do the following:

    //First check the current branch
    var currentBranch  = Umbraco.Content(Model.Path.Split(","));
    var firstNodeOfTypeD = currentBranch.FirstOrDefault(n=>n.ContentType.Alias.Equals("d"));
    
    if(firstNodeOfTypeD == null)
    {
        firstNodeOfTypeD = Model.Site().DescendantOfType("d");
    }
    

    However that implies the first occurrence means working down each branch.

    Another interpretation of this request could mean you need to return Page 2, because that is the "first occurrence of type D" when level is a higher priority.

    In this scenario,

      var firstOccuranceOfTypeD = Model.Site().DescendantsOfType("d").OrderBy(n=>n.Level).ThenBy(n=>n.SortOrder).FirstOrDefault();
    

    This version could also be done in examine.

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Aug 22, 2019 @ 11:35
    Paul Seal
    0

    How about:

    1. Model.AncestorOrSelf(1).DescendantsOrSelf<Article>();
    
    2. Model.AncestorOrSelf(1).DescendantsOrSelf<Article>().FirstOrDefault();
    

    Cheers

    Paul

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Aug 22, 2019 @ 11:37
    Paul Seal
    0

    I feel like mine would be slow in v7 but i'm led to believe it is quicker v8 this way

Please Sign in or register to post replies

Write your reply to:

Draft