简体   繁体   English

三级自动映射器关系

[英]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.

相关问题 第 3 个子级的 Linq 查询 - Linq query on 3rd child level 具有Include语句的实体框架,用于选择3级特定字段 - Entity Framework with Include statement to select 3rd level specific fields 在 Linq 查询上需要一些帮助,第三级包括 where - Need some help on a Linq query, 3rd level include where 实体框架多对多关系手动创建第三张表 - Entity Framework Many-Many Relationship Create 3rd Table Manually 包括第3层相关的集合,并使用代码优先实体框架在gridview中显示 - include 3rd level related collection and display in gridview with code-first entity framework 嵌套中继器-如何在不使用runat服务器标签的情况下访问第3级项目? - Nested repeaters - How to access item on 3rd level without runat server tag? 实体框架6在第3级子表中编码第一个一对多关系 - Entity Framework 6 Code first One-to-Many relation in 3rd level child tables 仅获取3级XML节点的名称LINQ C# - Get Just 3rd level XML nodes' names LINQ C# 除非 2 级嵌套组件具有事件,否则属性窗口不会显示 3 级嵌套组件的事件 - Properties Window does not display events for 3rd level nested components unless a 2nd level nested component has an event Automapper 多对多关系 - Automapper many - to - many relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM