Automapper 不将列表对象复制到另一个

Automapper not copy list object to another

我有我的 class 类型的列表,我需要将其复制到另一个 class 类型。在下面的代码中,我试图将 GenderEntity 复制到 Gender 但没有得到任何结果

private void MapGender()
    {
        List<GenderEntity> _GenderEntity = _GenderServiceObject.GenderEntity();

        List<Gender> _Gender = new List<Gender>();

        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<List<GenderEntity>, List<Gender>>();
        });

        IMapper mapper = config.CreateMapper();

        _Gender = mapper.Map<List<GenderEntity>, List<Gender>>(_GenderEntity);

        var x = "d";
    }

GenderEntity - 来源

public class GenderEntity
{
    public GenderEntity() { }

    public string Code { get; set; }

    public string Description { get; set; }

}

性别 - 目的地

 [DataContract]
public class Gender
{
    [DataMember]
    public string Code { get; set; }

    [DataMember]
    public string Description { get; set; }
}

性别实体用于 WCF SOAP 服务作为数据契约

Return 列表 Class

 public class GenderService
{

    #region EBS Service return 'Gender'  
    public Gender GenderEBSEntity()
    {
        return GenderEBS.GetGender_EBS();
    }
    #endregion

    #region Mapped Gender Entity
    public List<GenderEntity> GenderEntity()
    {        
           return GenderEBS.GetGenderEntity();            
    }
    #endregion

}

您不需要使用 AutoMapper 映射列表,只需像这样创建一个映射:

cfg.CreateMap<GenderEntity, Gender>();

然后像这样映射它:

 _Gender = mapper.Map<Gender[]>(_GenderEntity).ToList();

我的回答...

 private void MapGender()
    {
       List<Gender> MappedGender = new List<Contracts.Data.Gender>();

       IList<GenderEntity> genderEntity = _GenderServiceObject.GenderEntity();

        if(genderEntity!=null)
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<GenderEntity, Gender>();
            });

            IMapper mapper = config.CreateMapper();

            gender = mapper.Map<IList<GenderEntity>, IList<Gender>>(genderEntity).ToList();

        }

    }