开发者

ASP.NET MVC : Automapper to merge class

开发者 https://www.devze.com 2023-04-05 08:25 出处:网络
I have this code : public class OrderModel { public List<Order> Orders { get; set; } } public class Order

I have this code :

public class OrderModel
{
    public List<Order> Orders { get; set; }
}

public class Order
{
    public string Code { get; set; }
    public DateTime CreationDate { get; set; }
    public Customer Customer { get; set; }
}

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
开发者_C百科

I'd like get a List<MyClass> MyClass look like :

public class MyClass
{
    public string OrderCode { get; set; }
    public string OrderCreationDate { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Automapper can help me for this ? if no other solution to avoid loop ?

Thanks,


To do DTO flattening with automapper looks at this post and also this. They should answer your question.

If you don't want to use automapper I would use a simple Linq. Something like this

 var myClassList = (from p in OrderModel.Orders select new MyClass()
          {
            OrderCode = p.Code,
            OrderCreationDate = p.CreationDate,
            FirstName = p.Customer.FirstName,
            LastName = p.Customer.LastName
          }).ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消