[英]3rd Level Automapper Relationship
I'm integrating with a 3rd party API which is returning a complex data structure and in a part of it I have the following relationship. 我正在与一个第三方API集成,该API返回一个复杂的数据结构,在其中的一部分中,我具有以下关系。
public class Parent{
public List<SmartLink> SmartLink { get; set; }
}
The SmartLink
object looks like below: SmartLink
对象如下所示:
public class SmartLink {
public Address AddressInfo { get; set; }
}
I have tried to map it in several ways, one of them below, but I still get a null on the AddressInfo object. 我试图以几种方式映射它,以下是其中一种,但是在AddressInfo对象上仍然得到null。
cfg.CreateMap<Address, AddressInfo>();
cfg.CreateMap<Source, Parent>()
//This is not allowed since Automapper cannot map to 2nd level
.ForMember(d => d.SmartLink.AddressInfo, map => map.MapFrom(src => src.Smartlink.ToList().Select(addr => addr.Address)));
The line below works perfectly: 下面的行完美地工作:
.ForMember(d => d.SmartLink, map => map.MapFrom(s => s.Smartlink.ToList()))
How can I map/flatten a 3rd level property with Automapper, any pointers? 如何使用Automapper和任何指针映射/展平第三级属性?
I had overthought on it. 我对此有所考虑。 I simply added the following mapping and it worked. 我只是添加了以下映射,它就起作用了。
cfg.CreateMap<Address, AddressInfo>();
cfg.CreateMap<SmartlinkPart, SmartLink>(MemberList.Destination)
.ForMember(d => d.AddressInfo, map => map.MapFrom(s => s.Address));
The idea is that for the member AddressInfo
, the first line above will provide it's mapping instruction. 这个想法是,对于成员AddressInfo
,上面的第一行将提供它的映射指令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.