2011-10-14 1 views
1

나는 가입 약속을 남긴 고객 목록을 가져오고 있습니다. 모든 고객이 this answer에 따라 약속을하지 수 있기 때문에, 나는 다음과 같은 Automapper 구성했다 :Automapper 구성의 Null/MinValue 체크인

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(); 

그리고 DateTimeHelper는 간단하다 :

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)); 
     } 
    } 

내가 StartDateTime는 '1/1 /임을 확인 0001 12:00:00 AM ',하지만 어떻게 든 DateTime.MinValue에 대한 검사가 작동하지 않고 DateTimeHelper로 넘어가서 물론 예외를 throw합니다.

무엇이 누락 되었습니까?

답변

1

, 드디어 임시 해결책을 구현했습니다 :

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

    return thisDate; 
} 

이상적은 아니지만 일을 계속할 수 있습니다.

0

위 코드를 기반으로 대상 속성 : "StartDateTime"은 문자열입니다.

난 그냥 시계 창에 코드를 삽입하고, 여기에 당신이 무엇을 얻을 수 있습니다 :

귀하의 비교

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

내 비교

중 하나가 관심 경우
Name: "1/1/0001 12:00:00 AM" == DateTime.MinValue.ToString() 
Value: true 
Type: bool 
+0

src.StartDateTime 속성을 확인하는 것을 제외하고는 참이므로 ... – seekay

+0

Very true. 디버깅 할 때 무엇을 볼 수 있습니까? 사용해보십시오 DateTime.Compare (yourDate, DateTime.MinValue) 그리고 당신이 얻는 것을보십시오. yourDate min은 양수가됩니다. – drneel

+0

문제는 사용자가 실제로 시작할 때로드되는 AutoMapper 구성을 디버그 할 수 없다는 것입니다. 방법), 엔티티를 뷰 모델에 매핑 할 때 값이 전달됩니다. 디버그가 가능하다면 해결할 수 있습니다. – seekay