Source to Destination Automapper Map with Object List

I need to create a mapping to an automaton.

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

Destination

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

Person class

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

I am trying to create a mapping

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

but I don’t know how to match the name Firstname, Lastname with an array of the Person object

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

This solution should create a new instance Person, but wouldn’t it be better for you to map them to a new class, rather than an array?

+5
source

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 
                ;
0
source

All Articles