将条件映射为true或忽略ping

问题描述 投票:0回答:2

我在DTO和实体上有一个“ IsActive”字段。

 public class Dto {
    public int Id {get;set;}
    public bool IsActive {get;set;}
 }

 public class Entity {
    public int Id {get;set;}
    public bool IsActive {get;set;}
 }

而且我想在下面映射它

* Set value to true if Id == 0
* Ignore mapping if ID != 0

我该怎么办?

        CreateMap<Dto, Entity>(MemberList.Source)
            .ForMember(dto => dto.Active, options => options.Condition((dto, entity) => ??));

我将IsActive保留在[​​C0]中,因为我需要将其传递给客户端。

c# .net-core automapper
2个回答
1
投票

最终这就是我得到的

Dto

0
投票
            .ForMember(entity => entity.Active, options => options.MapFrom((dto, entity) => dto.Id == 0 ? true : entity.Active))
© www.soinside.com 2019 - 2024. All rights reserved.