How to configure Conditional Mapping in AutoMapper?
AutoMapper allows adding conditions to properties that must be met before that property will be mapped.
Mapper.CreateMap<Source,Target>()
.ForMember(t => t.Value, opt =>
{
opt.PreCondition(s => s.Value1.StartsWith("A"));
opt.MapFrom(s => s.Value1);
})
Try this
Mapper.CreateMap<Source, Target>()
.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.