linq如何订购此声明

时间:2016-08-02 16:50:43

标签: c# linq linq-to-sql

如您所见,我试图在声明结尾处执行多个order by语句。我的结果没有回复正确。

var query = 
    (from x in workloadDetail
    group x by new { x.titleOrder, x.httitle } into sortedData                        
    select new WorkloadSummary()
    {
    httitle = sortedData.Key.httitle,
    totalHrs = sortedData.Sum(x => x.totalHrs),
    totalDol = sortedData.Sum(x => x.totalDol),
    titleOrder = sortedData.Key.titleOrder
    }).OrderBy(x => x.httitle).OrderByDescending(x => x.totalHrs);

2 个答案:

答案 0 :(得分:5)

如果您要链接多个OrderBy()个查询,则需要使用ThenBy()ThenByDescending()作为后续属性,以便按正确的顺序应用它们:

.OrderBy(x => x.httitle).ThenByDescending(x => x.totalHrs);

答案 1 :(得分:4)

使用ThenByDescending方法按降序应用二级排序。

.OrderBy(x => x.httitle).ThenByDescending(x => x.totalHrs);