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

1 Answer 1

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);
5
  • 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<List<Customer>>(array)... why does it not?
    – Bjørn
    Jun 11, 2014 at 6:51
  • 2
    @Swell, my understanding is that autoMapper support all the generic collection types. To be specific here's the list: IEnumerable, IEnumerable<T>, ICollection, ICollection<T>, IList, IList<T>, List<T>, Arrays.
    – Bayeni
    Jun 11, 2014 at 6: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, 2014 at 7:13
  • @Bayeni it's true, but when you ask IEnumerable<T> as the destination what Automapper is supposed to return? Collection<T>? List<T>?
    – JuChom
    Jun 11, 2014 at 7:16
  • 1
    it will return the IEnumerable<DestinationType>, ie. IEnumerable<DestinationType> ienumerableDest = Mapper.Map<SourceType[], IEnumerable<DestinationType>>(sources); Thanks Swell
    – Bayeni
    Jun 11, 2014 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.