Search code examples
c#automapperautomapper-2

AutoMapper: Mapping child member from complex type to string[]


I have a destination type with a string[] property.

Animal
  string[] Barks;

My source object is:

  AnimalDTO
     List<BarkTypes> Barks;

How do I map BarkTypes.NameOfBark to the string[] Barks?

Something like this:?

Mapper.CreateMap<AnimalDTO, Animal>()
   .ForMember(dest => dest.Barks, y => y.MapFrom(x=>x.??????))

Solution

  • You want ResolveUsing:

    Mapper.CreateMap<AnimalDTO, Animal>()
          .ForMember(dest => dest.Barks,
                        y => y.ResolveUsing(x=>x.Barks
                                                .Select(b=>b.NameOfBark)
                                                .ToArray())
                  )