自动映射器映射到可为null的DateTime属性

编程入门 行业动态 更新时间:2024-10-28 03:19:15
本文介绍了自动映射器映射到可为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属性

本文发布于:2023-11-17 05:40:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609024.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可为   属性   映射器   null   DateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!