映射IEnumerable模型进行查看(Map IEnumerable model to view)

编程入门 行业动态 更新时间:2024-10-26 03:40:04
映射IEnumerable模型进行查看(Map IEnumerable model to view)

保持这个基本我有两个模型。 customer和CustomerViewModel。 我想将属性映射到另一个。

public class Customer { public string CompanyName { get; set; } public int CustomerType { get; set; } public IEnumerable<ContactViewModel> CustomerContacts { get; set; } } public class CustomerViewModel { public string CompanyName { get; set; } public int CustomerType { get; set; } public ContactName {get;set;} public ContactTel {get;set;} }

问题是我想将customerContact映射到contactName和contactTel。 我知道它是一个IEnumerable并包含一个联系人列表,但在此过滤器中IEnumerable的联系人视图模型将只包含1条记录。

我如何在automapper中进行这种类型的映射?

更新:

我决定和前面提到的类似approuch一起去。 我希望我可以在automapper中做到这一点,但我想它稍微复杂一些。

public static CustomerListViewModel MapToListView(this CustomerServiceModel svcModel) { ContactServiceModel contact = new ContactServiceModel { Name = String.Empty, Telephone = String.Empty }; if(svcModel.CustomerContacts != null) contact = svcModel.CustomerContacts.FirstOrDefault(); return new CustomerListViewModel { Id = svcModel.Id, address = svcModel.Address.MapToView(), CompanyName = svcModel.CompanyName, ContactName = contact.Name, ContactTelephone = contact.Telephone }; }

Keeping this basic I have two models. customer and CustomerViewModel. I want to map the properties between one to another.

public class Customer { public string CompanyName { get; set; } public int CustomerType { get; set; } public IEnumerable<ContactViewModel> CustomerContacts { get; set; } } public class CustomerViewModel { public string CompanyName { get; set; } public int CustomerType { get; set; } public ContactName {get;set;} public ContactTel {get;set;} }

The problem is I want to map customerContact to contactName and contactTel. I understand it is an IEnumerable and contains a list of contacts but during this filter IEnumerable of contact view model will only contain 1 record.

How would I do this type of mapping in automapper?

UPDATE:

I decided to go with a similar approuch someone mentioned earlier. I was hoping I can do this in automapper but I guess it is slightly more complex.

public static CustomerListViewModel MapToListView(this CustomerServiceModel svcModel) { ContactServiceModel contact = new ContactServiceModel { Name = String.Empty, Telephone = String.Empty }; if(svcModel.CustomerContacts != null) contact = svcModel.CustomerContacts.FirstOrDefault(); return new CustomerListViewModel { Id = svcModel.Id, address = svcModel.Address.MapToView(), CompanyName = svcModel.CompanyName, ContactName = contact.Name, ContactTelephone = contact.Telephone }; }

最满意答案

我不确定这可以通过AutoMapper完成。 您可以尝试定义扩展方法,例如:

public static CustomerViewModel ToViewModel(this Customer cust) { return new CustomerViewModel() { CompanyName = cust.CompanyName, CustomerType = cust.CustomerType, ContactName = cust.CustomerContacts.First().ContactName, ContactTel = cust.CustomerContacts.First().ContactTel }; }

当然,顶部的一点验证会很好。 然后你像这样使用它:

Customer cust= GetCustomer(); CustomerViewModel model= cust.ToViewModel();

I am not sure this can be done with AutoMapper. You can try to define an extension method, such as this:

public static CustomerViewModel ToViewModel(this Customer cust) { return new CustomerViewModel() { CompanyName = cust.CompanyName, CustomerType = cust.CustomerType, ContactName = cust.CustomerContacts.First().ContactName, ContactTel = cust.CustomerContacts.First().ContactTel }; }

Of course, a little validation on top would be nice. Then you use it like this:

Customer cust= GetCustomer(); CustomerViewModel model= cust.ToViewModel();

更多推荐

本文发布于:2023-08-06 07:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446008.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   IEnumerable   Map   view   model

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!