我需要按状态==“拒绝”和日期升序排序列表,然后按状态==“已接受”和日期降序排序。
我正在尝试以下但我不确定如何解决这个问题:
items
.OrderBy(x => x.status == "Rejected")
.ThenBy(x => x.DateSubmitted)
.ThenBy(x => x.status == "Accepted")
.ThenByDescending(x => x.DateSubmitted)
答案 0 :(得分:3)
你描述的任务有点尴尬,我认为这会导致你的实现略显错误。
我说:
items
.Where(x => x.status == "Rejected")
.OrderBy(x => x.DateSubmitted)
.Concat(items.
.Where(x => x.status == "Accepted")
.OrderByDescending(x => x.DateSubmitted));
主要区别在于,现在不显示“已拒绝”或“已接受”以外的状态。 但是,我认为这是你的意图。如果您想要全套,请考虑使用
items
.Where(x => x.status == "Rejected")
.OrderBy(x => x.DateSubmitted)
.Concat(items.
.Where(x => x.status != "Rejected")
.OrderByDescending(x => x.DateSubmitted));
PS。这也是假设Linq-To-Objects。我不太熟悉Linq-to-EF或Linq-to-SQL