我试图弄清如何将包含多个订单集合的商家集合展平为一个订单视图模型的列表。
这是我的数据传输对象:
public class Merchant
{
public string MerchantName { get; set; }
public List<Order> Orders { get; set; }
}
public class Order
{
public string OrderId { get; set; }
}
以下是视图模型:
public class OrderViewModel
{
public string MerchantName { get; set; }
public string OrderId { get; set; }
}
我的目标是将一个List<Merchant>扁平化为一个List<OrderViewModel>,测试结构应该产生6个视图模型:
var myMerchants = new List<Merchant>
{
new Merchant
{
MerchantName = "Merchant X",
Orders = new List<Order>
{
new Order { OrderId = "Order 1"},
new Order { OrderId = "Order 2"},
new Order { OrderId = "Order 3"}
}
},
new Merchant
{
MerchantName = "Merchant Y",
Orders = new List<Order>
{
new Order { OrderId = "Order 4"},
new Order { OrderId = "Order 5"},
new Order { OrderId = "Order 6"}
}
}
};
var models = Mapper.Map<List<OrderViewModel>>(myMerchants);