本文介绍了自动映射器映射到可为null的DateTime属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Automapper 3.1.1,我无法编译此映射:
using Automapper 3.1.1 I can not compile this map:
Mapper.CreateMap<Domain.DomainObjects.Entities.Patient, PatientDto>() .ForMember(x => x.Deleted, opt => opt.MapFrom(input => input.Deleted.HasValue ? new DateTime(input.Deleted.Value.Ticks, DateTimeKind.Utc) : null ));错误:
条件表达式的类型无法确定,因为'< null>'之间没有隐式转换和"DateTime"
实体:
public class Patient : Entity { // more properties public virtual DateTime? Deleted { get; set; } }感觉就像我遗漏了一些明显的东西,但无法确切地知道到底是什么.
Feel like I am missing something obvious but can not figure out what exactly.
注意:Dto包含 DateTime?也删除了
推荐答案我尚未测试,但是您只需要将 null 强制转换为 DateTime?.((DateTime?)null )
I haven't tested, but you should just need to explicitly cast null to a DateTime?. ((DateTime?)null)
Mapper.CreateMap<Domain.DomainObjects.Entities.Patient, PatientDto>() .ForMember(x => x.Deleted, opt => opt.MapFrom(input => input.Deleted == null ? (DateTime?)null : ( new DateTime(input.Deleted.Value.Ticks, DateTimeKind.Utc))));更多推荐
自动映射器映射到可为null的DateTime属性
发布评论