title: EF model class - Linq select to a non database property
body: <p>I've designed these below tables and I've mapped it using the EF 6, so when I try to select one Company with all documents and the individual type for each document, the EF returns the nested data from the ListCompanyDocumentsTypes. Ive tried some methods - see below</p>
  
  <p><strong>Tables</strong>
  <a href="https://i.stack.imgur.com/Ikxw3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ikxw3.png" alt="enter image description here"></a>
  <strong>1 - NOT GOOD - It Loads the company, documents, documentTypes and nested documents from the ListCompanyDocumentTypes</strong></p>
  
  <pre><code>    public static IQueryable&lt;Company&gt; CompleteCompanies(micwayEntities context)
      {
          return context.Companies
              .Include(l1 =&gt; l1.CompanyDocuments)
              .Include(l2 =&gt; l2.CompanyDocuments.Select(p=&gt; p.ListCompanyDocumentType));
      }
  </code></pre>
  
  <p>DATA RETURNED</p>
  
  <blockquote>
    <p>1) - Company</p>
    
    <p>2) - "List of CompanyDocument " from each company   </p>
    
    <p>3) - "CompanyDocumentType" for each CompanyDocument</p>
    
    <p>4) - "CompanyDocument" for each CompanyDocumentType</p>
  </blockquote>
  
  <p><strong>2 - WORKED - Non-Mapped from a Select</strong></p>
  
  <p>As I just need the "ListCompanyDocumentTypes > Name", so to solve this problem, I've created a NonMapped property within the CompanyDocument table to load it from the DB - see below; however, I am not sure if it is the correct way to do it.</p>
  
  <pre><code>public static IQueryable&lt;Company&gt; CompleteCompanies(micwayEntities context)
  {
      return context.Companies.Include(l1 =&gt; l1.CompanyDocuments);
  }
  </code></pre>
  
  <p>CompanyDocument.cs</p>
  
  <pre><code>    [NotMapped]
      public string NondbTypeName
      {
          get
          {
              using (var db = new micwayEntities())
              {
                  var listCompanyDocumentType = db.ListCompanyDocumentTypes.FirstOrDefault(p =&gt; p.id.Equals(typeId));
                  return listCompanyDocumentType?.name;
              }
          }
  
      }
  </code></pre>
  
  <p><strong>3 - DOESNT WORK - it throws an exception when the company is loaded DB</strong></p>
  
  <p>Also, I tried to create a Non-Mapped property within the CompanyDocument model class to try to load the "Name" from the "ListCompanyType" that is within the CompanyDocument model class, but it throws an exception as the EF doesn't load this information - see below</p>
  
  <pre><code>public static IQueryable&lt;Company&gt; CompleteCompanies(micwayEntities context)
  {
      return context.Companies.Include(l1 =&gt; l1.CompanyDocuments);
  }
  </code></pre>
  
  <p>CompanyDocument.cs</p>
  
  <pre><code>public partial class CompanyDocument
  {
      #region NonMappedProperties
  
      [NotMapped]
      public string NondbTypeName =&gt; ListCompanyDocumentType.name;
  
      #endregion
  
      public int id { get; set; }
  
      [Required]
      [StringLength(15)]
      public string companyAbn { get; set; }
  
      public int typeId { get; set; }
  
      [Required]
      [StringLength(50)]
      public string description { get; set; }
  
      [StringLength(50)]
      public string number { get; set; }
  
      [Column(TypeName = "date")]
      public DateTime? expiryDate { get; set; }
  
      [Required]
      [StringLength(255)]
      public string displayName { get; set; }
  
      [Required]
      [StringLength(255)]
      public string path { get; set; }
  
      public bool isArchived { get; set; }
  
      public bool isDeleted { get; set; }
  
      public DateTime createdOn { get; set; }
  
      [Required]
      [StringLength(50)]
      public string createdById { get; set; }
  
      public DateTime? lastChangedOn { get; set; }
  
      [StringLength(50)]
      public string lastChangeById { get; set; }
  
      public virtual Company Company { get; set; }
  
      public virtual ListCompanyDocumentType ListCompanyDocumentType { get; set; }
  
      public virtual Worker WorkerCreatedBy { get; set; }
  
      public virtual Worker WorkerLastChangeBy { get; set; }
  }
  </code></pre>
  
  <p><strong>My questions are:</strong></p>
  
  <ol>
  <li>Is the approach 2 correct?</li>
  <li>Or is there any way to limit nested objects through the Include / Select as described by the approach 1?</li>
  <li>Or Can I force the approach 3 to load the Name property without having to load all nested objects?</li>
  </ol>