[英]Automapper: use source and destination together in mapping
Is it possible, in Automapper, to concatenate the source with the destination when configuring a mapping of String properties? 在Automapper中,在配置String属性的映射时是否可以将源与目标连接起来? I thought I could just do something this:
我以为我可以这样做:
Mapper.CreateMap<Foo, Bar>()
.ForMember(d => d.Notes, opt => opt.MapFrom(s => d.Notes + s.Notes)));
...
Mapper.Map<Foo, Bar>(source, destination);
However, in the MapFrom lambda, d
obviously isn't in scope. 但是,在MapFrom lambda中,
d
显然不在范围内。 Any suggestions on how to combine the source and destination values in my mapping? 有关如何在我的映射中组合源值和目标值的任何建议?
You can do this with an AfterMap
which does the concatenation as follows: 您可以使用
AfterMap
执行此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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.