简体   繁体   English

从源到目标的自动映射映射,包含对象列表

[英]Automapper map from Source to Destination containing List of Objects

I have to create a Mapping with automapper. 我必须用automapper创建一个Mapping。

Public class Source
{
    public string Id;
    public string Firstname;
    public string Lastname;
}

Destination is 目的地是

Public class Destination
{
    public string Id;
    public Person[] persons;
}

Person Class is 人类是

Public class Person
{
    public string FirstName;
    public string LastName;
}

I am trying to create mapping 我正在尝试创建映射

AutoMapper.Mapper.CreateMap<Source, Destination>(); 

but I don't know how to map Firstname, Lastname to array of object Person. 但我不知道如何将Firstname,Lastname映射到对象Person的数组。

AutoMapper.Mapper.CreateMap<Source, Destination>().AfterMap((s,d) => d.Person = new Person[] { FirstName = s.FirstName, LastName = s.LastName }));

这个解决方案应该创建一个Person的新实例,但是你最好将它们映射到一个新类而不是一个数组吗?

I solved it . 我解决了

AutoMapper.Mapper.CreateMap<Source, Destination>()
                .AfterMap((s, d) => d.persons= new Person[1])
                .AfterMap((s, d) => d.persons[0] = new Person{ FirstName= s.FirstName, LastName= s.LastName, RemoteId = s.Name 
                ;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM