0

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;


    }
1
  • What type is orderLines (we are missing class for OrderHeader), and have you made sure orderLines isn't null or empty? Commented Mar 20, 2014 at 19:16

2 Answers 2

1

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(...
Sign up to request clarification or add additional context in comments.

8 Comments

I debuged ordersList, and it returns correct collection. when i map a list then AutoMapper knows automatically? error I get is : An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code
Ok. Is this exception new? It isn't mentioned in your original question. Also, is orderLines an IEnumerable<ProductsDTO>?
yes its IEnumerable, I updated my question please look at the orderHeader Class. Its same Exception.
All the Code I have I posted above already, I don't have any other mapping for OrderLines. I am new to AutoMapper.
Great! If property names and types are the same, you can exclude the forMember clauses because AutoMapper is smart enough to map those for you.
|
1

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.

2 Comments

I address this in my answer.
@chadiusvt Apologies, I think I was typing my answer when you were updating yours. I didn't see it initially.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.