I have a linq query that should output a date column from a string. The expression currently looks like this
myObject.OrderByDescending(s=> s.MyDate).Where(s => s.CRAStatus.Description == "CheckedOut").FirstOrDefault().MyDate)
The problem is that if there are no lines that are "CheckedOut", the request will return null and trying to get "MyDate" will throw an exception. We have several detailed solutions, such as:
.ForMember(dest => dest.CheckOutDate, opt => opt.MapFrom(src => { var temp = src.CRAStatusChangeEvents.OrderByDescending(s=> s.MyDate).Where(s => s.CRAStatus.Description == "CheckedOut").FirstOrDefault(); return temp == null ? temp.MyDate : null; }));
But it would be nice to find something more concise. Any ideas?
source share