Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq Order By String Not Working

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.

like image 277
tcode Avatar asked Feb 18 '26 17:02

tcode


1 Answers

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();
like image 161
Mike Nowak Avatar answered Feb 21 '26 15:02

Mike Nowak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!