将条件映射为真或忽略应用

map condition to true or ignore apping

我在 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 保存在 Dto 中,因为我需要将其传递给客户。

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

最后这就是我得到的

            .ForMember(entity => entity.Active, options => options.MapFrom((dto, entity) => dto.Id == 0 ? true : entity.Active))