Automapper 配置中的 Null/MinValue 检查

发布于 2024-12-10 06:29:17 字数 1606 浏览 2 评论 0原文

我正在提取一份离开加入约会的客户名单。由于所有客户可能没有预约,根据这个答案,我有以下 Automapper 配置

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 ,它当然会抛出异常。

我缺少什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灵芸 2024-12-17 06:29:17

如果有人感兴趣,我最终实施了一种解决方法:

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

   return thisDate;
}

不理想,但可以完成工作,我可以继续前进。

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.

万劫不复 2024-12-17 06:29:17

根据上面的代码,您的目标属性:“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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文