Copied to clipboard

Flag this post as spam?

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


  • Gary 77 posts 354 karma points
    Jun 29, 2016 @ 06:50
    Gary
    0

    Use Where to count a specific amount of child items that have a certain property

    Hi All,

    I currently have a list of "News Items" which are shown on the "News Page". Each "News Item" has a "Borough" and "Target Audience" meta tags on them.

    For example: News Item 1 - is for Manchester and the target audience is Children.

    How can i get each distinct value for borough and then count how many times that value shows up.

    This is something i have at the moment but however the count seems a bit off... Well a lot.

    @{  
    
    List<string> boroughs = CurrentPage.Children.Pluck("borough");
    foreach(var item in @boroughs.distinct()) {
        <a href="?Borough=@item" style="color: #000;"><p stle="color: #000;">@item <span class="badge" style="float: right;">@item.Count()</span></p></a>
    }
    

    }

    To put this request into perspective here is a screen shot of some navigation i have created where i want to use the count of items.

    Navigation demo

    If there is anythign you guys can provide to help or point me in the right direction!

    Kind Regards,

    Gary

  • David Peck 587 posts 1554 karma points c-trib
    Jun 29, 2016 @ 07:32
    David Peck
    1

    I think you're after GroupBy so something like this:

    List<IPublishedContent> boroughGroups = CurrentPage.Children.GroupBy(c => c.GetPropertyValue<string>("borough"));
    foreach(var boroughGroup in boroughGroups) {
       <a href="?Borough=@boroughGroup.Key" style="color: #000;"><p stle="color: #000;">@boroughGroup.Key <span class="badge" style="float: right;">@boroughGroup.Count()</span></p></a>
    }
    

    Or something to that effect

    FYI: Never knew about 'Pluck'. Thanks for that.

  • Gary 77 posts 354 karma points
    Jun 29, 2016 @ 07:42
    Gary
    0

    Hi David,

    Your Welcome!

    However, this looks like it may solve my problem. I have come across an issue.

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

    Is this something you can assist with?

    Thanks again for the help!

    Cheers,

    Gary

  • David Peck 587 posts 1554 karma points c-trib
    Jun 29, 2016 @ 08:10
    David Peck
    2

    Probably. CurrentPage is a dynamic node. I'm going to assume this code is in Razor and your view inherits from UmbracoTemplatePage or UmbracoViewPage. In which case just swap CurrentPage for Model.Content. Model.Content is an IPublishedContent object and you should then be away.

    Also you may need to change the Children property to a method: Children() ?

  • Gary 77 posts 354 karma points
    Jun 29, 2016 @ 08:20
    Gary
    100

    Got it!!

    Changed it from this:

    List<IPublishedContent> boroughGroups = CurrentPage.Children.GroupBy(c => c.GetPropertyValue<string>("borough"));
    foreach(var boroughGroup in boroughGroups) {
       <a href="?Borough=@boroughGroup.Key" style="color: #000;"><p stle="color: #000;">@boroughGroup.Key <span class="badge" style="float: right;">@boroughGroup.Count()</span></p></a>
    }
    

    To this:

    @{  
    
        var boroughGroups = Model.Content.Children().GroupBy(c => c.GetPropertyValue<string>("borough"));
        foreach (var item in @boroughGroups.Distinct()) {
    
            <a href="?Borough=@item.Key" style="color: #000;"><p stle="color: #000;">@item.Key <span class="badge" style="float: right;">@item.Count()</span></p></a>
        }
    }
    

    Works like a treat!

    Thank you very much for your help!

    Kind Regards,

    Gary

Please Sign in or register to post replies

Write your reply to:

Draft