开发者

AutoMapper : Copy IList<> to IList<>

开发者 https://www.devze.com 2023-03-13 20:49 出处:网络
I have an IList<AdminVAT> and I\'d like to copy this collection to IList<AdminVATDto> collection

I have an IList<AdminVAT> and I'd like to copy this collection to IList<AdminVATDto> collection

I tried this :

IList<AdminVAT> listAdminVAT = new AdministrationService(session).ListDecimal<AdminVAT>();
AutoMapper.Mapper.CreateMap<IList<AdminVAT>, List<AdminVATDTO>>();
var res = AutoMapper.Mapper.Map<IList<AdminVAT>, IList<AdminVATDTO>>(listAdminVAT);

I receive this exception :

Trying to map System.Collections.Generic.IList`1[[AdminVAT, eSIT.GC.DataModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.IList`1[[AdminVATDTO, eSIT.GC.WebUI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].

Thanks.

Update1

public class AdminVAT : IAdminDecimal
{
    public virtual int Id { get; set; }
    public virtual int Code { get; set; }
    public virtual decimal Value { get; set; }
}
public class AdminVATDTO : AdminVAT
{
    [DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
    public override decimal Value { get; set; }
}

I still have 5 decimal in my dropdown 开发者_Python百科list ...

Controller :

IList<AdminVAT> listAdminVAT = new AdministrationService(session).ListDecimal<AdminVAT>();
AutoMapper.Mapper.CreateMap<AdminVAT, AdminVATDTO>();
model.ListVAT = AutoMapper.Mapper.Map<IList<AdminVAT>, IList<AdminVATDTO>>(listAdminVAT);

HTML :

@Html.DropDownList("ddVAT", new SelectList(Model.ListVAT, "Id", "Value", Model.Estimation.AdminVAT))


Define the mapping only between the simple types as explained in the documentation:

AutoMapper.Mapper.CreateMap<AdminVAT, AdminVATDTO>();

Then you will be able to convert between lists, collections, enumerables of those types:

IList<AdminVATDTO> res = AutoMapper.Mapper.Map<IList<AdminVAT>, IList<AdminVATDTO>>(listAdminVAT);
0

精彩评论

暂无评论...
验证码 换一张
取 消