How to configure Conditional Mapping in AutoMapper?

后端 未结 4 1402
花落未央
花落未央 2020-12-13 17:48

Suppose I have the following entities (classes)

public class Target
{
    public string Value;
}


public class Source
{
    public string Value1;
    public         


        
4条回答
  •  暖寄归人
    2020-12-13 18:19

    Try this

     Mapper.CreateMap()
            .ForMember(dest => dest.Value, 
                       opt => opt.MapFrom
                       (src => src.Value1.StartsWith("A") ? src.Value1 : src.Value2));
    

    Condition option is used to add conditions to properties that must be met before that property will be mapped and MapFrom option is used to perform custom source/destination member mappings.

提交回复
热议问题