Reputation: 368
I need to order a list by status == "Rejected" and date ascending and then by status == "Accepted" and date descending.
I am trying the following but I'm not sure how to go about this:
items
.OrderBy(x => x.status == "Rejected")
.ThenBy(x => x.DateSubmitted)
.ThenBy(x => x.status == "Accepted")
.ThenByDescending(x => x.DateSubmitted)
Upvotes: 3
Views: 171
Reputation: 392833
You're describing the task a bit awkward, which (I think) leads you to a slightly wrong implementation.
I say:
items
.Where(x => x.status == "Rejected")
.OrderBy(x => x.DateSubmitted)
.Concat(items.
.Where(x => x.status == "Accepted")
.OrderByDescending(x => x.DateSubmitted));
The main difference is that now, status other than "Rejected" or "Accepted" are not shown. However, I think it is what you intended. If you want the full set, consider using
items
.Where(x => x.status == "Rejected")
.OrderBy(x => x.DateSubmitted)
.Concat(items.
.Where(x => x.status != "Rejected")
.OrderByDescending(x => x.DateSubmitted));
PS. This is also assuming Linq-To-Objects. I'm not very well-versed with Linq-to-EF or Linq-to-SQL
Upvotes: 3