Automapper IEnumerable<something> to IEnumerable<somethingelse> without creating a map configuration

Bj&#248;rn picture Bjørn · Jun 11, 2014 · Viewed 14.6k times · Source

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

Answer

JuChom picture JuChom · Jun 11, 2014

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);