I want to convert one List to another using AutoMapper, but it returns an empty list
This is my source object
public class Charge
{
public int ChargeID { get; set; }
public string ChargeName { get; set; }
public int CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public int ModifiedBy { get; set; }
public DateTime ModifiedDate { get; set; }
}
This is my destination object
public class ChargeVM
{
public int ChargeID { get; set; }
public string ChargeName { get; set; }
public bool IsActive { get; set; }
}
This is my AutoMapper mapping
List<Charge> chargeList= GetActiveChargeTemplates();
Mapper.CreateMap<List<Charge>, List<ChargeVM>>();
List<ChargeVM> list=Mapper.Map<List<Charge>, List<ChargeVM>>(chargeList);
this return list as an empty list with count=0.
Am I doing something wrong here?
Change
Mapper.CreateMap<List<Charge>, List<ChargeVM>>();
to
Mapper.CreateMap<Charge, ChargeVM>();
You don't have to explicitly create maps for list, only the objects in the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With