I'm pulling a list of customers left join appointments. Since all customers may not have appointments, based on this answer, I have the following Automapper configuration:
Mapper.CreateMap<Event, EventDetailsViewModel>()
.ForMember(dest => dest.StartDateTime, opt => opt.MapFrom(
src => src.StartDateTime == DateTime.MinValue ? "" : DateTimeHelper.ConvertFromUtc(src.StartDateTime, src.TimeZoneId)
.ToString("MM/dd/yyyy hh:mm tt", System.Globalization.CultureInfo.InvariantCulture)))
.ForMember(dest => dest.EndDateTime, opt => opt.MapFrom(
src => src.StartDateTime == DateTime.MinValue ? "" : DateTimeHelper.ConvertFromUtc(src.EndDateTime, src.TimeZoneId)
.ToString("MM/dd/yyyy hh:mm tt", System.Globalization.CultureInfo.InvariantCulture)))
.IgnoreAllNonExisting();
And the DateTimeHelper is straightforward:
public static class DateTimeHelper
{
public static DateTime ConvertToUtc(DateTime thisDate, string timeZoneId)
{
return TimeZoneInfo.ConvertTimeToUtc(thisDate, TimeZoneInfo.FindSystemTimeZoneById(timeZoneId));
}
public static DateTime ConvertFromUtc(DateTime thisDate, string timeZoneId)
{
return TimeZoneInfo.ConvertTimeFromUtc(thisDate, TimeZoneInfo.FindSystemTimeZoneById(timeZoneId));
}
}
I verified that the StartDateTime is '1/1/0001 12:00:00 AM', but somehow the check for DateTime.MinValue isn't working and it gets over to the DateTimeHelper which of course then throws an exception.
What am I missing?