1

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?

Community
  • 1
  • 1
seekay
  • 2,493
  • 3
  • 27
  • 33

2 Answers2

1

In case any one is interested, I finally implemented a workaround in place:

public static DateTime ConvertFromUtc(DateTime thisDate, string timeZoneId)
{
   if (!String.IsNullOrEmpty(timeZoneId)) // workaround
      return TimeZoneInfo.ConvertTimeFromUtc(thisDate, TimeZoneInfo.FindSystemTimeZoneById(timeZoneId));

   return thisDate;
}

Not ideal, but does the job and I can move on.

seekay
  • 2,493
  • 3
  • 27
  • 33
0

Based on the code above, your destination property: "StartDateTime" is a string.

I just put the code into a watch window, and here is what you get:

Your comparison

Name:  "1/1/0001 12:00:00 AM" == DateTime.MinValue  
Value: Operator '==' cannot be applied to operands of type 'string' and 'System.DateTime'

My comparison

Name:  "1/1/0001 12:00:00 AM" == DateTime.MinValue.ToString()   
Value: true 
Type:  bool
drneel
  • 2,887
  • 5
  • 30
  • 48
  • True, except I'm checking the src.StartDateTime property, so it should be valid... – seekay Oct 14 '11 at 21:38
  • Very true. What do you see when you debug? Try and use DateTime.Compare(yourDate, DateTime.MinValue) and see what you get. yourDate < min will give negative number yourDate = min will give 0 yourDate > min will give positive number – drneel Oct 14 '11 at 22:44
  • The problem is you can't really debug the AutoMapper configuration as that's loaded when the app starts (at least I don't know how to), but the values are passed in when you map the entity to the viewmodel. If debug is possible, then solving it would be straightforward :) – seekay Oct 14 '11 at 23:35
  • Right, but you can debug it by manually doing the comparison before your Mapper.Map call. – drneel Oct 15 '11 at 00:08
  • (myobj.StartDateTime == DateTime.MinValue) = true, and DateTime.Compare(myobj.StartDateTime, DateTime.MinValue) = 0 – seekay Oct 15 '11 at 00:15