Sunday, 15 February 2015

How to use Automapper to project into new list -


I can not find an example of doing this. I'm new to automaker. I may have used the wrong terminology.

  public class stomach {public int petit {receives; Set;} public string name {get; Set; } Public string breed {get; Set; }} Public class person {public string first name {receipt; Set; } Public String LastName {get; Set; } Public listing & lt; Pet & gt; Pets {receive; Set; }} Public personVM {public string first name {get; Set; } Public String LastName {get; Set; } List & lt; PetDisplay & gt; PetDisplay {get; Set}} Public PetDisplay {int petID {get; Set;} string petDetails {get; Set} // It should be a string like the stomach. Name & amp; "|" & Amp; Pet.Breed}  

How do people map to personVM? Thanks

You have to set up a mapping with person PersonVM and pet PetDisplay :

  Mapper.cartmap < Person, PersonVM & gt; (.) For mail (dest = & gt; dest.PetDisplay, opt => opt.MapFrom (src = & gt; src.Pets)); Mapper.CreatMap & lt; Pet, PetDisplay & gt; (.) For mail (dest = & gt; dest.petDetails, opt = & gt; opt.MapFrom (src = & gt; string.format ("{0} | {1}", src.Name, Src. Breed))); Example Use:  
  var person = new person {first name = "john", last name = "skeet", pet = new list & Lt; Pet & gt; {New pet {breed = "lab", name = "feedo"}}}; PersonVM vm = Mapper Map & lt; PersonVM & gt; (person);  Example:   

PS: I recommend following the NET Framework Naming Guidelines and use Pascal Season for category and member names ( Personal < / Code> instead of personVM ).


No comments:

Post a Comment