Rama
Rama

Reputation: 307

AutoMapper Return an empty list when Mapping two lists

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?

Upvotes: 3

Views: 3940

Answers (1)

HansVG
HansVG

Reputation: 789

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.

Upvotes: 8

Related Questions