在配置String属性的映射时,是否可以在Automapper中将源与目标连接起来?我以为我可以做点什么:
Mapper.CreateMap<Foo, Bar>()
.ForMember(d => d.Notes, opt => opt.MapFrom(s => d.Notes + s.Notes)));
...
Mapper.Map<Foo, Bar>(source, destination);
但是,在MapFrom lambda中,d
显然不在范围内。关于如何在我的映射中组合源值和目标值的任何建议?
答案 0 :(得分:12)
您可以使用AfterMap
执行此操作,该连接执行以下连接:
Mapper.CreateMap<Foo, Bar>()
.ForMember(dest => dest.Notes, opt => opt.Ignore())
.AfterMap((src, dest) => dest.Notes = string.Concat(dest.Notes, src.Notes));
var source = new Foo { Notes = "A note" };
var destination = new Bar { Notes = "B note" };
Mapper.Map<Foo, Bar>(source, destination);
Console.WriteLine(destination.Notes);
<强> Working Fiddle 强>