Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper failing to map on IEnumerable

Tags:

c#

automapper

You do not need to explicitly map collection types, only the item types. Just do:

Mapper.CreateMap<SentEmailAttachment, SentEmailAttachmentItem>();
var attachments = Mapper.Map<IEnumerable<SentEmailAttachment>, List<SentEmailAttachmentItem>>(someList);

That will work just fine.


EDIT: I found an easy way to use DynamicMap with collections.

IEnumerable<FakeItem> unmappedItems = Repository.GetItems();
IEnumerable<MappedItem> mappedItems = unmappedItems.Select(Mapper.DynamicMap<MappedItem>);

— Original message —

The way Jimmy says to use it works, but I try to use DynamicMap when I can to avoid having to do "CreateMap" for every mapping I need. I don't think DynamicMap works with collections very well, if at all. It does not throw an exception, but the result is an empty set.

From testing over the past couple of days, you cannot use DynamicMap for collections at this time (that I know of).