Copied to clipboard

Flag this post as spam?

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


  • Levente Kosa 136 posts 352 karma points
    Dec 13, 2019 @ 13:15
    Levente Kosa
    0

    Orderby with property value problem

    Hi,

    I would like to order children by a property value. Here are my structure: enter image description here

    What I want is to group by awardYear, so at the end under a year there would be many awardTitle etc. E.g:

    2019

    • award title 1
    • award title 2
    • award title 3

    2018

    • award title 4
    • award title 5

    2016

    • award title 6
    • award title 7
    • award title 8

    When I try like this...

    var awards = Model.Root().Children.Where(x => x.IsDocumentType("awards")).FirstOrDefault();
            foreach (var award in awards.Children.Where(x => x.IsVisible()).GroupBy(x => x.Value<string>("awardYear")).OrderByDescending(x => x.Value("awardYear")))
            {
                var awardYear = award.Value("awardYear");
                var awardTitle = award.Value("awardTitle", fallback: Fallback.ToDefaultValue, defaultValue: award.Name);
    
                <div class="col-4">
                    <h4 class="text-uppercase">@awardYear</h4>
                    <p>@awardTitle, @awardYear</p>
                </div>
    
    
            }
    

    ...I got this error:

     'IGrouping<string, IPublishedContent>' does not contain a definition for 'Value' and the best extension method overload 'ValueExtensions.Value(HtmlHelper, string)' requires a receiver of type 'HtmlHelper'
    

    A little help would be great from somebody. Thanks!

  • Lars Heesakkers 38 posts 194 karma points
    Dec 13, 2019 @ 13:59
    Lars Heesakkers
    0

    I think you need to use the "GetPropertyValue" instead of the "Value" function.

  • Levente Kosa 136 posts 352 karma points
    Dec 13, 2019 @ 14:14
    Levente Kosa
    0

    It wasn't that, but thanks

  • Lars Heesakkers 38 posts 194 karma points
    Dec 13, 2019 @ 14:16
    Lars Heesakkers
    0

    Could you share your solution for others to see in the future?

  • Levente Kosa 136 posts 352 karma points
    Dec 13, 2019 @ 14:40
    Levente Kosa
    0

    Sorry, there is no solution yet, I just answered that "GetPropertyValue" wasn't good. I got this message with it:

    'IPublishedContent' does not contain a definition for 'GetPropertyValue' and no accessible extension method 'GetPropertyValue' accepting a first argument of type 'IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
    
  • Lars Heesakkers 38 posts 194 karma points
    Dec 13, 2019 @ 14:42
    Lars Heesakkers
    1

    Do you have the following using: @using Umbraco.Web at the top of your file?

  • Levente Kosa 136 posts 352 karma points
    Dec 13, 2019 @ 15:02
    Levente Kosa
    0

    I didn't have to, but I just figured out luckily. Here is the solution if somebody needs it:

    foreach (var award in awards.Children.Where(x => x.IsVisible()).OrderByDescending(x => x.Value("awardYear")).GroupBy(x => x.Value<string>("awardYear")))
        {
            <h4 class="text-uppercase">@award.Key</h4>
            foreach (var item in award)
            {
                var awardYear = item.Value("awardYear");
                var awardTitle = item.Value("awardTitle", fallback: Fallback.ToDefaultValue, defaultValue: item.Name);
    
                <div class="col-4">
                    <p>@awardTitle, @awardYear</p>
                </div>
            }
    
        }
    
  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Dec 13, 2019 @ 15:05
    Marc Goodson
    101

    Hi Levente

    It's because you've used 'GroupBy'

    Your now looping through an IEnumerable<>

    instead of an IEnumerable

    so your award variable in the foreach loop is an IGrouping

    var awards = Model.Root().Children.Where(x => x.IsDocumentType("awards")).FirstOrDefault();
            foreach (var awardGroup in awards.Children.Where(x => x.IsVisible()).GroupBy(x => x.Value<string>("awardYear")).OrderByDescending(x => x.Value("awardYear")))
            {
    // you've grouped all of the awards for a particular year underneath the  awardYear - which is represented by the 'Key' of the group:
                var awardYear = awardGroup.Key;
    
       <div class="col-4">
                    <h4 class="text-uppercase">@awardYear</h4>
    //now your 'group' will contain all the award's IPublishedContent items for that year so you can loop through these:
    @foreach (var award in awardGroups){
                var awardTitle = award.Value("awardTitle", fallback: Fallback.ToDefaultValue, defaultValue: award.Name);
                    <p>@awardTitle, @awardYear</p>
    
    }
           </div>  
            }
    

    if that makes sense, it's been a while since I've grouped anything...

    regards

    Marc

  • Levente Kosa 136 posts 352 karma points
    Dec 13, 2019 @ 15:06
    Levente Kosa
    0

    Thank you Mark, I just found it as well :)

Please Sign in or register to post replies

Write your reply to:

Draft