Automapper failing to map on IEnumerable

后端 未结 2 1423
情歌与酒
情歌与酒 2021-02-02 05:31

I have two classes like so:

public class SentEmailAttachment : ISentEmailAttachment
{
    public SentEmailAttachment();

    public string FileName { get; set; }         


        
相关标签:
2条回答
  • 2021-02-02 06:29

    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.

    0 讨论(0)
  • 2021-02-02 06:33

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

    0 讨论(0)
提交回复
热议问题