简体   繁体   English

自动映射:在映射中一起使用源和目标

[英]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);

Working Fiddle 工作小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于源和目标的 AutoMapper 条件映射 - AutoMapper Conditional Mapping based on source and destination Automapper - 目的地的条件映射 - Automapper - conditional mapping on destination 使用Automapper进行自定义映射,其中目标中的字段是源中两个字段的串联 - Custom mapping with Automapper where a field in destination is the concatenation of two fields in source Automapper - 从源子 object 到目标的映射包括父值 - Automapper - Mapping from source child object to destination is including parent values 从源映射到现有目标时,AutoMapper 不会忽略 List - AutoMapper does not ignore List when mapping from source to existing destination Automapper 将多个列表从源映射到目标上的单个列表 - Automapper mapping multiple lists from source to single list on destination Automapper 仅在映射对象列表时覆盖不在源中的目标值 - Automapper overrinding destination values that are not in source ONLY when mapping lists of objects Automapper继承的源和目标 - Automapper Inherited Source and Destination 如何使用AutoMapper将目标对象与源对象中的子对象进行映射? - How to use AutoMapper to map destination object with a child object in the source object? 如何使用automapper将源类型的Parent映射到目标类型的集合? - How to use automapper to map Parent of source type into a collection of destination type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM