Guest User

Untitled

a guest
Jun 30th, 2012
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. How to MapFrom a collection? Auto mapper
  2. Mapper.CreateMap<A, G>();
  3.  
  4.  
  5. // A is the linq to sql object
  6. A.MyList // this is a collection that I am trying to get the value out of A.MyList.Id
  7.  
  8. // G is my View Model
  9. public class G
  10. {
  11. public string AMyListId {get; set;}
  12.  
  13. }
  14.  
  15.  
  16. vm = Mapper.Map<List<A>, List<G>>(aListOfAFromDb);
  17.  
  18. A.Vip.UserId // again my linq object is A
  19.  
  20. // G is my View Model
  21. public class G
  22. {
  23. public string IsVip {get; set;}
  24.  
  25. }
  26.  
  27.  
  28. vm = Mapper.Map<List<A>, List<G>>(aListOfAFromDb);
  29.  
  30. Mapper.CreateMap<A, G>().ForMember(dest => dest.IsVip, opt => opt.AddFormatter<VipFormatter>());
  31.  
  32.  
  33. public class VipFormatter : IValueFormatter
  34. {
  35. public string FormatValue(ResolutionContext context)
  36. {
  37. bool value = (bool)context.SourceValue;
  38.  
  39. if (value)
  40. {
  41. return "Yes";
  42. }
  43.  
  44.  
  45. return "No";
  46.  
  47. }
  48. }
  49.  
  50. List<A> dbItems;
  51. IEnumerable<G> results = dbItems.Select(x => x.MyList.MyListID);
  52.  
  53. Mapper.CreateMap<A, G>().ForMember(dest => dest.IsVip, opt => opt.MapFrom(src => src.Vip == null));
  54.  
  55. List<G> items = // whatever
  56. var result = items.Select(g => Mapper.Map<G, A>(g));
Advertisement
Add Comment
Please, Sign In to add comment