Copied to clipboard

Flag this post as spam?

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


  • Robin Nicholl 137 posts 277 karma points
    Mar 29, 2016 @ 14:49
    Robin Nicholl
    0

    List distinct property in foreach loop

    Hi, I'm trying to simply list (for navigation) a unique/distinct property named "category":

    <ul>
        <li>Category1</li>
        <li>Category2</li>
    </ul>
    

    Many of the nodes I'm looping through will contain the same "category" property, but I only want it to appear once in the list, eg NOT what I'm getting at the moment, which is…

    <ul>
        <li>Category1</li>
        <li>Category1</li>
        <li>Category2</li>
        <li>Category1</li>
        <li>Category2</li>
        <li>etc…</li>
    </ul>
    

    I assumed it would be something like this:

    var selection = CurrentPage.Children.Where("Visible");
    foreach( var page in selection.Distinct("category") ) {
        <li><a href="@page.category">@page.category</a></li>
    }
    

    I know this won't work(!), but hopefully it illustrates what I'm trying to do.

    Thanks

    Robin

  • TheOriginal 22 posts 122 karma points
    Mar 29, 2016 @ 15:46
    TheOriginal
    0

    How many pages do you have? Is it duplicating the same ones or is there actually that amount just wrong text?

  • Robin Nicholl 137 posts 277 karma points
    Mar 29, 2016 @ 15:53
    Robin Nicholl
    0

    There's a variable number of pages under this node. Currently 6 categories, but more than one of the pages will have the same category. I use a foreach loop to display the pages, and another foreach loop to create the 'navigation': it's this which needs to show each of the categories in use by the current selection of pages retrieved… but only once per category: currently I'm getting every instance of the category property. Eg:

    page1 category="category1" page2 category="category1" page3 category="category4"

    So I show all the pages, 1 to 3, but only "category1" and "category4" in the nav, not "category1, category1, category4"…

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Mar 29, 2016 @ 16:10
    Nicholas Westby
    0

    I would not recommend dynamic syntax. Instead, try this:

    var pages = Model.Content.Children.Where(x => x.IsVisible())
        .DistinctBy(x => x.GetPropertyValue<string>("category"));
    
  • Robin Nicholl 137 posts 277 karma points
    Mar 29, 2016 @ 17:21
    Robin Nicholl
    0

    Thanks, Nicholas, but I never use Model, only ever CurrentPage.etc… so I have no idea how to implement this :(

    I currently have something like:

    var promoSelection = CurrentPage.Children.Where("Visible");
    foreach( var page in promoSelection ) {
        @* do stuff *@
    }
    

    … and I'm not sure how I'd use the non-dynamic syntax to operate on children of the current page.

  • Robin Nicholl 137 posts 277 karma points
    Mar 29, 2016 @ 17:36
    Robin Nicholl
    0

    OK, so…

    var pages = Model.Content.Children.Where(x => x.IsVisible()).DistinctBy(x => x.GetPropertyValue<string>("category"));
    foreach( var page in pages ) {
        <li><a href="@what?">@what</a></li>
    }
    

    If I put @page inside foreach loop it prints out 'UMBRACO.WEB.PUBLISHEDCACHE…'

    I'd have thought this would work…

    @page.GetPropertyValue<string>("category")
    

    … but it gives a compilation error

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Mar 29, 2016 @ 20:45
    Nicholas Westby
    0

    What's the compilation error? Try assigning it to a variable first. For example:

    var category = page.GetPropertyValue<string>("category");
    @category
    

    By the way, @page.Url will give you the page's URL.

  • Robin Nicholl 137 posts 277 karma points
    Mar 30, 2016 @ 11:54
    Robin Nicholl
    0

    Thanks for your help, Nicholas. Unfortunately I don't see a way of getting the value of the property rather than a number. At the moment I'm getting 133, 135 instead of Eat, Shop…

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Apr 03, 2016 @ 23:23
    Nicholas Westby
    0

    Looks like you are getting the prevalues (basically, a number representing the value). Here's how you get the text version of a prevalue:

    var categoryPreValue = Model.Content.GetPropertyValue<int?>("category");
    var category = categoryPreValue.HasValue ? umbraco.library.GetPreValueAsString(categoryPreValue.Value) : null;
    
Please Sign in or register to post replies

Write your reply to:

Draft