Reputation: 1180
I am trying to do a OrderByDescending on my LINQ query. Currently I am doing OrderByDescending on ProcedureDate field.
Business Scenario: A user can come in the application and add ChangeProcedureDates in application upto 3 times. So in database there are additional fields called ChangeProcedureDate1, ChangeProcedureDate2 and ChangeProcedureDate3.
What I am trying to accomplish: I want to modify my LINQ query so it can check to see if in cases where ChangeProcedureDate1, ChangeProcedureDate2 and ChangeProcedureDate3 are NULL then OrderBy ProedureDate since there were no changes. If ProcedureDate, ChangeProcedureDate1 is filled and ChangeProcedureDate2 and ChangeProcedureDate3 are NULL then we need to OrderBy ChangeProcedureDate1. And same for ChangeProcedureDate2 and ChangeProcedureDate3. Basically I need to find the latest date entry among multiple DateTimeFields and do orderby on that.
Below is my LINQ query.
var unsortedItems = context.ReservationRequests
.Where(s => ((s.ReservationRequestStatusId.HasValue) &&
(s.ReservationRequestStatusId.Value == ReservationRequestStatusId)) &&
(Roles.Any(y => s.SubmitterGroupName == y) ||
s.CreatedBy == currentUserGuid))
.OrderByDescending(x => x.ProcedureDate ?? DateTime.MinValue);
I am having hard time trying to understand how can I do this in LINQ?
Upvotes: 0
Views: 376
Reputation: 24913
Try this:
.OrderByDescending(x => x.ChangeProcedureDate3 ?? x.ChangeProcedureDate2 ?? x.ChangeProcedureDate1 ?? x.ProcedureDate);
Or, change orders of properties as you need.
Upvotes: 1