9

I cannot get the following to work, where array is an array of CustomerContract's:

Mapper.Map<IEnumerable<Customer>>(array);

Mapper.Map<IEnumerable<CustomerContract>, IEnumerable<Customer>>(array);

Mapper.Map<Array, List<Customer>>(array);

In my mind the first example should be enough, but i can not get either to work. I have read the configuration wiki of automapper (https://github.com/AutoMapper/AutoMapper/wiki/Configuration), but i do not understand why this should be necessary. Everything Automapper needs is defined in the command. Which type it is (both object and that it is a list), and which object i want it to map to.

Am i just not understanding the core concept of Automapper?

My exception sounds like this:

Missing type map configuration or unsupported mapping.
Mapping types:\r\nCustomerContract -> Customer\r\nStimline.Xplorer.Repository.CustomerService.CustomerContract -> Stimline.Xplorer.BusinessObjects.Customer
Destination path: List`1[0]
Source value: Stimline.Xplorer.Repository.CustomerService.CustomerContract

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
Bjørn
  • 1,138
  • 2
  • 16
  • 47

1 Answers1

17

You're mapping to IEnumerable... Automapper can map to a concrete type not an interface.

First register your mapping (see the "Missing type map configuration or unsupported mapping") You must use CreateMap once for performance

Mapper.CreateMap<something, somethingelse>();

Instead of:

Mapper.Map<IEnumerable<Customer>>(array);

Try this:

Mapper.Map<List<Customer>>(array);

or

Mapper.Map<Customer[]>(array);
Pac0
  • 21,465
  • 8
  • 65
  • 74
JuChom
  • 5,717
  • 5
  • 45
  • 78
  • Thanks, it worked. But i still don't understand why i need to create the map... it should be able to determine all of the info CreateMap is giving it from the inputs of the Mapper.Map>(array)... why does it not? – Bjørn Jun 11 '14 at 06:51
  • 2
    @Swell, my understanding is that autoMapper support all the generic collection types. To be specific here's the list: IEnumerable, IEnumerable, ICollection, ICollection, IList, IList, List, Arrays. – Bayeni Jun 11 '14 at 06:51
  • 1
    @BjørnØyvindHalvorsen because CreateMap is actually a slow method... If CreateMap was called everytime you call Map it would kill your application performance – JuChom Jun 11 '14 at 07:13
  • @Bayeni it's true, but when you ask IEnumerable as the destination what Automapper is supposed to return? Collection? List? – JuChom Jun 11 '14 at 07:16
  • 1
    it will return the IEnumerable, ie. IEnumerable ienumerableDest = Mapper.Map>(sources); Thanks Swell – Bayeni Jun 11 '14 at 07:30