Linq 按字符串排序不起作用

Linq Order By String Not Working

我不明白为什么这段代码不按字符串名称对我的数据列表进行排序。

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();

我正在尝试按字符串标签对列表进行排序,但是,它永远行不通。

谁能看出我做错了什么?

提前致谢。

您正在使用 ToList() 对列表进行排序并创建新列表,但您没有将 return 值分配给任何东西,所以您丢失了它。通过以下方式修复它:

result = result.OrderByDescending(x => x.label).ToList();

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();