I would like to order children by a property value. Here are my structure:
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!
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?)
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...
Orderby with property value problem
Hi,
I would like to order children by a property value. Here are my structure:
What I want is to group by awardYear, so at the end under a year there would be many awardTitle etc. E.g:
2019
2018
2016
When I try like this...
...I got this error:
A little help would be great from somebody. Thanks!
I think you need to use the "GetPropertyValue" instead of the "Value" function.
It wasn't that, but thanks
Could you share your solution for others to see in the future?
Sorry, there is no solution yet, I just answered that "GetPropertyValue" wasn't good. I got this message with it:
Do you have the following using: @using Umbraco.Web at the top of your file?
I didn't have to, but I just figured out luckily. Here is the solution if somebody needs it:
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
if that makes sense, it's been a while since I've grouped anything...
regards
Marc
Thank you Mark, I just found it as well :)
is working on a reply...