Donald C

Donald C

  • 1.5k
  • 153
  • 3.2k

Using Automapper \ Linq .Include to retrieve specific data

Jul 2 2017 5:54 PM
Hi Guys,
 
Im working on an old db.
How do I go about configuring Automapper to retrieve the contacts primary email address into the Person class? 
How do I structure the .Include subquery so that it only retrieves the first email address (ordered by isPrimary)? 
 
 
 Any help would be appreciated.
 
Thanks 
 
  1. pulic class Contact  
  2. {  
  3.     public string AccountId { getset; }  
  4.     public string Fname { getset; }  
  5.     public string LName { getset; }  
  6. //....other fields      
  7.     public string RecordId { getset; }  
  8. }  
  9.   
  10. public class Misc  
  11. {  
  12.     public string AccountId { getset; }  
  13.     public string Rectype {getset; }  
  14.     public string Suplamental { getset; }  
  15.     public string isPrimary { getset; }  
  16. //....other fields      
  17.     public string RecordId { getset; }  
  18.       
  19. }  
  20.   
  21. public class Person  
  22. {  
  23.     public string AccountId { getset; }  
  24.     public string Fname { getset; }  
  25.     public string LName { getset; }  
  26.     public string PrimaryEmail {getset; }  
  27.     public string RecordId { getset; }  
  28. }  
  29.   
  30.   
  31.   
  32.     class Program  
  33.     {  
  34.         static void Main(string[] args)  
  35.         {  
  36.   
  37.             ICollection<Contact> Contacts = new List<Contact>();  
  38.             Contacts.Add(new Contact("1""Bob""Fender""101"));  
  39.             Contacts.Add(new Contact("2""Dave""Jones""102"));  
  40.   
  41.             ICollection<Misc> Miscs = new List<Misc>();  
  42.             Miscs.Add(new Misc("1""E""[email protected]""1""101"))   
  43.             Miscs.Add(new Misc("1""E""[email protected]""0""102"))   
  44.             Miscs.Add(new Misc("1""L""C:\Docs\Bob.doc""0""103"))   
  45.             Miscs.Add(new Misc("2""E""[email protected]""1""104"))   
  46.             Miscs.Add(new Misc("2""W""www.daveswebsite.com""0""105"))   
  47.             Miscs.Add(new Misc("2""L""C:\Docs\Daves.doc""0""106"))   
  48.   
  49.   
  50.             context.Contacts  
  51.             .Include(Misc).Where(m => m.Rectype == 'E').OrderbyDescending(isPrimary).FirstOrDefault()  
  52.         }  
  53.     }  
  54.   
  55.    
  56. }