Copied to clipboard

Flag this post as spam?

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


  • Kim Bantz Rasmussen 81 posts 310 karma points
    Feb 06, 2013 @ 15:01
    Kim Bantz Rasmussen
    0

    Random node by nodeTypeAlias - hide current page

    Hi fine folks,

    I have this code, which is working great at picking two random nodes (Persons). But I would love if I could hide/filter "Person 1" if I am visiting that page or children of that page. 

    Page structure:

    Home
    -- Person 1
    -- -- History 1
    -- -- History 2
    -- Person 2
    -- -- History 3
    -- Person 3
    -- -- History 4

    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{ 
    var r = new Random();
    var numberOfItems = 2;
    
    List<DynamicNode> pages = new DynamicNode(Model.AncestorOrSelf("Home"))
        .Descendants("Person")
        .Items.Where(x => x.Visible)
        .OrderBy(x => r.Next())
        .Take(numberOfItems)
        .ToList();
    }
    
    
    <div class="row">
        @foreach (DynamicNode page in pages)
        {       
            <div class="six columns">
                <img src="http://placehold.it/350x150" />
                <h2>@page.Name</h2>
                <p>@page.GetProperty("introText")</p>
                <p><a href="@page.Url">Read more...</a></p>
                <p><strong>@page.GetProperty("personsName")</strong></p>                       
            </div>
        }   
    </div>


    Best regards!
    Kim

     

  • Niels Jorck Ellegaard 39 posts 245 karma points
    Feb 07, 2013 @ 12:05
    Niels Jorck Ellegaard
    0

    Hi Kim

    If I understand  you correctly you just want to filter out the Person one in your DynamicNode list. You can do that in various ways depending on what suits you best.

    List<DynamicNode> pages =new DynamicNode(Model.AncestorOrSelf("Home"))
           
    .Descendants("Person")
           
    .Items.Where(x => x.Visible)
           
    .OrderBy(x => r.Next())
           
    .Take(numberOfItems)
           
    .ToList();
    }

    After .Descendants("Person") you can skip one with .Skip(1) that should remove the first one.

    Or sort it out with a .Where(x => x.Name == "Person1").

    Some of it might not be entirely correct as I didn't open VS to check, but should give you some pointers :)

  • Kim Bantz Rasmussen 81 posts 310 karma points
    Feb 07, 2013 @ 13:11
    Kim Bantz Rasmussen
    0

    Hi Niels,

    Thank you very much for your answer! 

    But I want to filter out 'Person1' if I am visiting that node or child nodes of 'Person1'. Similar if I am visiting 'Person2' or on of Person2 child nodes. It has to show two other persons :)

    Best regards
    Kim 

  • Niels Jorck Ellegaard 39 posts 245 karma points
    Feb 07, 2013 @ 13:28
    Niels Jorck Ellegaard
    0

    Hi Kim

    That makes a lot of sense. Well then you want to test if the DynamicNode is either the the same as the node your on or if it fits the nodes ancestors.

    Some freehand coding that can get you started:

    List<DynamicNode> pages = new DynamicNode(Model.AncestorOrSelf("Home"))
           
    .Descendants("Person").Where(x => !Model.AncestorsOrSelf().Any(y => y.Id == x.Id)
           
    .Items.Where(x => x.Visible)
           
    .OrderBy(x => r.Next())
           
    .Take(numberOfItems)
           
    .ToList();

    I try to get the Model (The node you are on). Test if any of the ancestors or the node itself has an Id that matches the id of the node you try to iterate. And only take those that doesn't match.

  • Kim Bantz Rasmussen 81 posts 310 karma points
    Feb 07, 2013 @ 14:21
    Kim Bantz Rasmussen
    0

    Hi Niels,

    I think it's really close ...

    List<DynamicNode> pages = new DynamicNode(Model.AncestorOrSelf("Home"))
        .Descendants("Person").Where(x => !Model.AncestorsOrSelf().Any(y => y.Id == x.Id))
        .Items.Where(x => x.Visible)
        .OrderBy(x => r.Next())
        .Take(numberOfItems)
        .ToList();
    }

    I have added a ')' at the end of line 2, but I get this error:

    CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

    Hope you can help me once more :)

    Best regards

  • Niels Jorck Ellegaard 39 posts 245 karma points
    Feb 07, 2013 @ 14:25
    Niels Jorck Ellegaard
    0

    I can't wait till I get better. Being sick really messes up my brain.

    List<DynamicNode> pages =newDynamicNode(Model.AncestorOrSelf("Home"))
           
    .Descendants("Person")
           
    .Items.Where(x => x.Visible && !Model.AncestorsOrSelf().Any(y => y.Id== x.Id))
            .OrderBy(x => r.Next())
            .Take(numberOfItems)
            .ToList();

    I think this works. :)

  • Kim Bantz Rasmussen 81 posts 310 karma points
    Feb 07, 2013 @ 14:46
    Kim Bantz Rasmussen
    0

    Hi again,

    I sincerely thank you for your time!

    It still comes with the same error :(

    @using umbraco.MacroEngines;
    
    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{ 
    var r = new Random();
    var numberOfItems = 2;
    
    
    List<DynamicNode> pages = new DynamicNode(Model.AncestorOrSelf("Home"))
            .Descendants("Person")
            .Items.Where(x => x.Visible && !Model.AncestorsOrSelf().Any(y => y.Id == x.Id))
            .OrderBy(x => r.Next())
            .Take(numberOfItems)
            .ToList();
    } 

     

  • Niels Jorck Ellegaard 39 posts 245 karma points
    Feb 07, 2013 @ 15:00
    Niels Jorck Ellegaard
    100

    I hate working with dynamic. I love my intellisense way too much.

    I can't even remember how it is with the versions anymore. Do you have uQuery? You might need some usings for uQuery, not sure.

    This might work:

    var r = new Random();
    var numberOfItems = 2;
    
    List<DynamicNode> pages = new DynamicNode(Model.AncestorOrSelf("Home"))
            .Descendants("Person")
            .Items.Where(x => x.Visible && uQuery.GetCurrentNode().GetAncestorOrSelfNodes().All(y => y.Id != x.Id))
            .OrderBy(x => r.Next())
            .Take(numberOfItems)
            .ToList();
    }
  • Kim Bantz Rasmussen 81 posts 310 karma points
    Feb 07, 2013 @ 15:43
    Kim Bantz Rasmussen
    0

    And it works with uQuery!

    Thank you - I owe you a giant beer!

  • Niels Jorck Ellegaard 39 posts 245 karma points
    Feb 07, 2013 @ 15:45
    Niels Jorck Ellegaard
    0

    I'll take a beer any day ;) Glad I could help.

Please Sign in or register to post replies

Write your reply to:

Draft