title: How to Improve on this Entity Framework Query
body: <p>I have the following entities: </p>
  
  <ul>
  <li>Applicant : Person</li>
  <li>PhoneNumber </li>
  <li>PhoneType</li>
  </ul>
  
  <p>The applicant is derived from <code>Person</code>. The <code>Applicant</code> has many <code>PhoneNumber</code>s (up to three). Each <code>PhoneNumber</code> has only 1 <code>PhoneType</code>.</p>
  
  <pre><code>public class Applicant : Person
  {    
      public virtual ICollection&lt;PhoneNumber&gt; PhoneNumbers { get; set; }
  }
  
  public class PhoneNumber 
  {                        
      public int PersonId { get; set; }
  
      [Required]
      [MaxLength(17)]
      public string Number { get; set; }    
  
      public int PhoneTypeId { get; set; }            
  
      public virtual PhoneType PhoneType { get; set; }
  
  }
  
  public class PhoneType 
  {     
      [Required]
      [MaxLength(24)]
      public string Name { get; set; }
  
      public virtual ICollection&lt;PhoneNumber&gt; PhoneNumbers { get; set; }
  }
  </code></pre>
  
  <p>It seems to me that the two queries I am using could be merged and the foreach loop eliminated, but I already have a large number of joins in the first query and I am not sure how to merge them. The question is a two part question: </p>
  
  <ol>
  <li>How could I merge the two queries and </li>
  <li>If I don't merge the queries, how could I improve upon the <code>PhoneNumber</code> / <code>PhoneType</code> query. </li>
  </ol>
  
  <p>The goal is to return an <code>Applicant</code> with a list of <code>PhoneNumber</code>s, and each <code>PhoneNumber</code> with a <code>PhoneType</code>.  For clarity the types are : Home, Office and Mobile.</p>
  
  <pre><code>/*------------------------------------------*/
  /* Obtain the Applicant                     */
  /*------------------------------------------*/
  IQueryable&lt;Applicant&gt; applicantQuery = 
        DbContext.Applicants.Where(a =&gt; a.CreatedBy == userId)
                 .Include(applicant =&gt; applicant.Address)
                 .Include(applicant =&gt; applicant.Address.StateProvince)
                 .Include(applicant =&gt; applicant.PhoneNumbers)                                                
                 .Include(applicant =&gt; applicant.HomeChurch)
                 .Include(applicant =&gt; applicant.HomeChurch.Address)
                 .Include(applicant =&gt; applicant.HomeChurch.Address.StateProvince)
                 .Include(applicant =&gt; applicant.TripApplication);
  
  Applicant applicant = applicantQuery.FirstOrDefault&lt;Applicant&gt;();
  
  if (applicant != null &amp;&amp; applicant.PhoneNumbers != null)
  {
      IQueryable&lt;PhoneType&gt; phoneTypeQuery = DbContext.Set&lt;PhoneType&gt;();
      List&lt;PhoneType&gt; phoneTypes = phoneTypeQuery.ToList&lt;PhoneType&gt;();
  
      foreach (PhoneNumber ph in applicant.PhoneNumbers)
      {
          ph.PhoneType = (phoneTypes.Where(pt =&gt; pt.Id == ph.PhoneTypeId)).First();
      }
  }
  </code></pre>
  
  <p>Thank you in advance for any help you might give. </p>