c# - AutoMapper mapping nested List object -
i trying map nested list dto object, not working. here tried. returns null. error is: an exception of type 'automapper.automappermappingexception' occurred in automapper.dll 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 how 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 sure problem relies in line: .formember(h => h.productsdto, k => k.mapfrom((m => m.orderlines))); productsdto , orderlines both lists.
here orderheader class:
public class orderheader { public orderheader() { // set 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; }
your automapper map declaration correct.
can verify orderslist.orderlines not null or empty before map it? possible ordersrepository isn't giving or populating orderslist way think is.
ienumerable<orderheader> orderslist = new ordersrepository().getorders(); // check value of orderslist.orderlines.count here // (assuming orderlines ienumerable<t> update
after verifying orderlines not empty, possible change productsdto declaration to:
public ienumerable<productsdto> productsdto { get; set; } there not reason specify list<>. possible getting type mismatch exception, because aren't able cast whatever orderlines list<>
update 2
you trying map ienumerable<orderlines> ienumerable<productsdto>, aren't congruant types. have map also?
you need well:
mapper.createmap<orderline, productsdto>().formember(...
Comments
Post a Comment