Copied to clipboard

Flag this post as spam?

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


  • Matthew 93 posts 153 karma points
    Jun 25, 2012 @ 08:52
    Matthew
    0

    Filter list of nodes by document type

    I have a tree 6 levels deep. I want to create an alphabetical list of bottom level nodes from any of the upper level ones. This:

    @foreach (var item in Model.DescendantsOrSelf(6).OrderBy("Name"))


    returns all the bottom level nodes but I'd rather filter it by document type, in case I ever stick another level on something or otherwise change it.

    I thought it'd be as simple as Model.DescendantsOrSelf(Model.MyDocumentTypeName) or perhaps .Where("MyDocumentTypeName") but I guess not.  I tried dinking around with .NodeTypeAlias but couldn't figure that out either.

    Any tips/pointers/suggestions?an't seem to get it to filter for just the bottom level node

  • Dmitriy Skudnov 39 posts 64 karma points
    Jun 25, 2012 @ 09:29
    Dmitriy Skudnov
    0

    Why this not working for you?

    @foreach (var item in Model.DescendantOrSelf("YOUR DOCUMENT TYPE").OrderBy("Name")) 

    ? Actually it should return all Descendants for current node with "YOUR DOCUMENT TYPE" document type.

     Also i think you can do filtering like:

    Model.DescendantOrSelf(6).Where(n => n.NodeTypeAlias == "YOUR DOCUMENT TYPE").OrderBy("Name")
  • Douglas Ludlow 210 posts 366 karma points
    Jun 25, 2012 @ 16:41
    Douglas Ludlow
    0

    Dmitriy is correct, you should be able to filter the collection of descendants by doing:

    var collection = Model.DescendantsOrSelf("MyDocumentTypeAlias").OrderBy("Name");

    However, using Where, you would do:

    var collection = Model.DescendantsOrSelf().Where("NodeTypeAlias == @0", "MyDocumentTypeAlias").OrderBy("Name");

     

  • Matthew 93 posts 153 karma points
    Jun 25, 2012 @ 17:53
    Matthew
    0

    You are right, Dmitriy, it does work.  I swear I tried that last night but maybe I missed it.  Thank you.

    Just to check whether I'm dumb or crazy, in other posts, I've run accross comments about Umbraco caching problems, is it possible it wasn't working last night due to something like that?  And if so, is that something that can be easily checked/cleared?

    Thanks much to both of you, I appreciate the suggestions and usage examples very much.

  • RGoodSW 5 posts 25 karma points
    Jun 26, 2012 @ 02:27
    RGoodSW
    0

    Any idea if there's a way to do a "greater than" or "less than" comparison in the where clause?  Using angle brackets caused the macro to fail to load in all of the cases I tried.

    --Bob G.

  • Matthew 93 posts 153 karma points
    Jun 26, 2012 @ 04:24
    Matthew
    0

    If you haven't seen it yet, according to the Razor walkthrough you can:

    http://umbraco.com/follow-us/blog-archive/2011/3/1/umbraco-razor-feature-walkthrough-%E2%80%93-part-4

    Cheat sheet mentions conditional filtering:

    http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet

    Personally, I haven't had the opportunity to try it so I can neither confirm nor deny.

  • Douglas Ludlow 210 posts 366 karma points
    Jun 26, 2012 @ 04:34
    Douglas Ludlow
    0

    @RGoodSW, you can you "greater than" and "less than" comparisons in the Where clause. It would be best if you gave an example so we could have an idea of why it's not working. Here are a couple of samples of how it should work:

    var root = Model.AncestorOrSelf();

    int level = 2;
    var collection1 = root.Descendants().Where("Level >= @0", level);

    //Or this:
    DateTime yesterday = DateTime.Today.AddDays(-1);
    var collection2 = root.Descendants().Where("UpdateDate > @0", yesterday);
Please Sign in or register to post replies

Write your reply to:

Draft