linq按两个标准排序

时间:2014-06-09 20:34:18

标签: c# linq

我在从linq查询中获得预期结果时遇到了一些麻烦。

以下是一些示例数据:

Queue Priority TTRDate
Main Critical 6/9/2014 7:41:00 PM
Main Major    6/9/2014 7:42:00 PM
Main Critical 6/9/2014 7:43:00 PM
Main Minor    6/9/2014 7:30:00 PM
Main Minor    6/9/2014 7:35:00 PM

查询:

listTickets = queryAllTickets.Where(y => y.Queue == "Main")
  .OrderBy(t => t.Priority == "Critical")
  .ThenBy(x => x.TTRDate)
  .Take(numberOfrecords)
  .ToList();

预期结果:

Main Critical 6/9/2014 7:41:00 PM
Main Critical 6/9/2014 7:43:00 PM
Main Minor    6/9/2014 7:30:00 PM
Main Minor    6/9/2014 7:35:00 PM
Main Major    6/9/2014 7:42:00 PM

简单来说,我想要通过TTRdate订购的关键物品,然后我想要TTRDate订购的所有其他物品。

1 个答案:

答案 0 :(得分:4)

false小于true,因此您需要OrderByDescending

.OrderByDescending(t => t.Priority == "Critical")

或使用!=代替==

.OrderBy(t => t.Priority != "Critical")