This project is read-only.

How to map from one generic list to another with different item types?

May 25, 2010 at 9:13 PM

Is there a way to map a generic list of T to another generic list of U?

For example:
var mapper = ObjectMapperManager.DefaultInstance.GetMapper<List<Order>, List<OrderSummary>>();

When I try to do the above, I get an error upon mapping that states:
"Unable to cast object of type 'Order' to type 'System.Collections.Generic.List`1[Order]'."


To get around it now, I am mapping each item and building a new list:

var mapper = ObjectMapperManager.DefaultInstance.GetMapper<Order, OrderSummary>();
var summaries = new List<OrderSummary>(orders.Count);
orders.ForEach(o => summaries.Add(mapper.Map(o)));


Is there a nicer, more compact way to handle this sort of thing?