AutoMapper for a list scenario only seems to repeat mapping the first object in the list

风格不统一 提交于 2019-12-04 13:18:29

So, a suitable solution, but not what we desire when using the AutoMapper.

This problem is common when you incorrectly override the Equals method of the entity/model being mapped.

For example, if you try to map a list of the objects above, you will get just the first object from the SourceEntity.

    public class SourceEntity 
    {
         public string MyField {get; set;}         

         public override bool Equals(object obj)
         {
              return true;
         }
    }

    public class TargetEntity 
    {
          public string MyField {get; set;}  
    }

Check that the Equals method is returning true.

user1790300

For anyone else with this problem, it appears as if the documentation was not working for me. A colleague made the following suggestion:

userSearchModel.UserList = UserEvent.Select(item => Mapper.Map<User, UserListModel>(item));

It worked like a charm.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!