I can't understand why this code won't order my list of data by string name.
public class GroupedRow
{
public int id { get; set; }
public string label { get; set; }
public decimal SumOfDays { get; set; }
}
var data = _dataService.GetData();
List<GroupedRow> result = data
.GroupBy(l => l.listItemID.Value)
.Select(cl => new GroupedRow
{
label = cl.First().ListItem.description,
SumOfDays = cl.Sum(c => c.timeAssigned.Value) / 8.0m
}).ToList();
result.OrderByDescending(x => x.label).ToList();
I am trying to order the list by label which is a string, however, it never works.
Can anyone see what I am doing wrong?
Thanks in advance.
You are ordering the list and creating the new list using the ToList() but you are not assigning return value to anything so you lose it. Eiter fix it by:
result = result.OrderByDescending(x => x.label).ToList();
or
List<GroupedRow> result = data
.GroupBy(l => l.listItemID.Value)
.Select(cl => new GroupedRow
{
label = cl.First().ListItem.description,
SumOfDays = cl.Sum(c => c.timeAssigned.Value) / 8.0m
})
.OrderByDescending(x => x.label)
.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With