c# - Map IEnumerable model to view -


I have two models to keep this origin. I mapping properties between one customer and client viewmodel Want

public class customer {public string company name {get; Set; } Type the public int customer {get; Set; } Public IEnumerable & lt; ContactViewModel & gt; Customer contact {receive; Set; }} Public class customer view model {public string CompanyName {get; Set; } Type the public int customer {get; Set; } Public contactName {find; Set;} public ContactTel {find; Set;}}

The problem is that I want to mapContact Map to CONTACTNAME and contactTel. I understand it is an IEnumerable and is included in the list of contacts but it will include only 1 record during IEnumerable of filter contact visual model.

How would I do this type of mapping in automapper?

Update:

I had previously decided to go with such an article. I expected that I can do this in automapper but I think it's a bit more complex

  Public Stable CustomerListViewModel MapToListView (this CustomerServiceModel svcModel) {ContactServiceModel Contact = New ContactServiceModel {name = String.Empty, Telephone = string.Alty}; If (svcModel.CustomerContacts! = Null) contact = svcModel.CustomerContacts.FirstOrDefault (); New CustomerListViewModel returned {id = svcModel.Id, address = svcModel.Address.MapToView (), CompanyName = svcModel.CompanyName, ContactName = contact.Name, ContactTelephone = contact.Telephone}; }  

I'm not sure that this can be done with AutoMapper.

  Public Stable CustomerViewModel ToViewModel (Customer Clients From) {New CustomerViewModel () Return {CompanyName = cust.CompanyName, CustomerType = cust.CustomerType: You try to define an extension method like this Can, contact name = cust.CustomerContacts.First () ContactName, ContactTel = cust.CustomerContacts.First (). ContactTel}; }  

Of course, little verification will be good at the top. You then use it like this:

  Customer Customer = GetCustomer (); Customer View Model Model = cust.ToViewModel ();  

Comments