gpt4 book ai didi

c# - Automapper 映射到可为空的 DateTime 属性

转载 作者:行者123 更新时间:2023-12-04 00:32:48 25 4
gpt4 key购买 nike

使用 Automapper 3.1.1 我无法编译这张 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 ));

错误:
Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'DateTime'
实体:
public class Patient : Entity
{
// more properties
public virtual DateTime? Deleted { get; set; }
}

感觉我错过了一些明显的东西,但无法弄清楚究竟是什么。

注意:Dto 包含 DateTime? Deleted

最佳答案

我还没有测试过,但你应该只需要显式转换 nullDateTime? . ( (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))));

关于c# - Automapper 映射到可为空的 DateTime 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48551696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com