I have a class Appointment that contains a nullable NodaTime.OffsetDateTime property. If I have a list of Appointments, how would I use Linq OrderBy to order this list with the null StartDateTime first then StartDateTime in descending order?
Class Appointment
{
public OffsetDateTime? StartDateTime { get; set; }
}
I tried doing this
var orderedAppointments = myAppointmentsList
.OrderByDescending(x => x.StartDateTime is null)
.ThenByDescending(x => x.StartDateTime, OffsetDateTime.Comparer.Local)
.ToList();
but I get an error because the OffsetDateTime.Comparer cant handle null values.
I could do something like this
var orderedAppointments = myAppointmentsList
.OrderByDescending(x => x.StartDateTime ?? StartDateTime.MaxValue, OffsetDateTime.Comparer.Local)
.ToList();
but StartDateTime doesn't have MaxValue.
What is the recommendation here, create my own OffsetDateTime max value, or is there a better way of doing this that I'm missing?