Question

I am trying to Map a nested List with my DTO object, however its not working. here is what I tried. everything returns null. Error I get is: An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code

public class ordersview_Dto
{
    public int orderID { get; set; }
    public string orderNumber { get; set; }
    public float? orderTotal { get; set; }
    public string lastName { get; set; }
    public string firstName { get; set; }
    public List<ProductsDTO> ProductsDTO { get; set; }

}

public class ProductsDTO
{


    public int id { get; set; }

    public int OrderID { get; set; }

 }

This is how I am mapping:

        Mapper.CreateMap<OrderHeader, ordersview_Dto>()
        .ForMember(h => h.ProductsDTO, k => k.MapFrom((m => m.orderLines)));

        IEnumerable<OrderHeader> ordersList = new OrdersRepository().getOrders();

        IEnumerable<ordersview_Dto> ordersFlow = Mapper.Map<IEnumerable<OrderHeader>, IEnumerable<ordersview_Dto>>(ordersList);

I am sure problem relies in this line: .ForMember(h => h.ProductsDTO, k => k.MapFrom((m => m.orderLines))); ProductsDTO and OrderLines both are Lists.

here is my OrderHeader Class:

 public class OrderHeader
    {

        public OrderHeader()
        {
            // Set any defaults here;
            orderLines = new List<OrderLines>();
        }


        public float? SubTotal { get; set; }

        public float? Shipping { get; set; }

        public string ShipMethod { get; set; }

        public float? Tax { get; set; }



        public int SalesID { get; set; }

        public string IpAddress { get; set; }

        // Mapping

        public Customer customer;
        public OrderCC ordercc;
        public OrderAddress orderAddress;
        public List<OrderLines> orderLines;


    }
Was it helpful?

Solution

Your AutoMapper map declaration is correct.

Can you verify that ordersList.orderLines is not null or empty before you map it? It is possible that your OrdersRepository isn't giving or populating ordersList the way that you think it is.

IEnumerable<OrderHeader> ordersList = new OrdersRepository().getOrders();
// Check value of ordersList.orderLines.Count here
// (assuming orderLines is an IEnumerable<T>

Update

After verifying that orderLines is not empty, possible change ProductsDTO declaration to:

public IEnumerable<ProductsDTO> ProductsDTO { get; set; }

There is not a reason to specify it as List<>. It is possible you are getting a type mismatch exception, because you aren't able to cast whatever orderLines is to a List<>


Update 2

You are trying to map IEnumerable<OrderLines> to IEnumerable<ProductsDTO>, which aren't congruant types. Do you have a map for this also?

You need something like this as well:

Mapper.CreateMap<OrderLine, ProductsDTO>().forMember(...

OTHER TIPS

You also need to Create a mapping to handle whatever class your orderLines collection contains, to ProductDTO. AutoMapper does't know how to handle those, so is ignoring them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top