Automapper 配置中的 Null/MinValue 检查
我正在提取一份离开加入约会的客户名单。由于所有客户可能没有预约,根据这个答案,我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果有人感兴趣,我最终实施了一种解决方法:
不理想,但可以完成工作,我可以继续前进。
In case any one is interested, I finally implemented a workaround in place:
Not ideal, but does the job and I can move on.
根据上面的代码,您的目标属性:“StartDateTime”是一个字符串。
我只是将代码放入监视窗口中,这就是您得到的内容:
您的比较
我的比较
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
My comparison