1

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.??????))
Ian Vink
  • 66,960
  • 104
  • 341
  • 555

2 Answers2

3

Completely untested, but:

Mapper.CreateMap<AnimalDTO, Animal>()
   .ForMember(dest => dest.Barks, 
              y => y.MapFrom(x=>x.Barks
                                 .Select(z => z.NameOfBark)
                                 .ToArray());
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
Martin
  • 11,031
  • 8
  • 50
  • 77
3

You want ResolveUsing:

Mapper.CreateMap<AnimalDTO, Animal>()
      .ForMember(dest => dest.Barks,
                    y => y.ResolveUsing(x=>x.Barks
                                            .Select(b=>b.NameOfBark)
                                            .ToArray())
              )
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Is this method safer than using Martin's answer with a simple Select? – Ian Vink Feb 21 '13 at 18:44
  • I don't think using `MapFrom` will work since that method requires an expression that points to a property. Other than that they should be the same. – D Stanley Feb 21 '13 at 18:52