title: How to use Automapper to flatten list of entity hierarchies?
body: <p>I want to use automapper to flatten a list of entity heirarchies returned back from Entity Framework Core.</p>
  
  <p>Here are my entities:</p>
  
  <pre><code>public class Employee {
      public int Id { get; set; }
      [Required]
      public string Name { get; set; }
      public double? PayRateRegular { get; set; }
      public double? PayRateLoadedRegular { get; set; }
      public double? GMOutput { get; set; }
      public string EmployeeType { get; set; }
  
      //List of CommissionDetails where this employee is the consultant
      public IEnumerable&lt;CommissionDetail&gt; CommissionDetailConsultants { get; set; } = new List&lt;CommissionDetail&gt;();
  }
  
  public class Project {
  
      public int Id { get; set; }
      public string Description { get; set; }
      public double? BillRateRegular { get; set; }
      public DateTime? StartDate { get; set; }
      public DateTime? EndDate { get; set; }
  
      public Customer Customer { get; set; }
      public int CustomerId { get; set; }
  
  }
  
  public class Customer {
  
      public int Id { get; set; }
      public string Name { get; set; }
  
  }   
  
  public class CommissionDetail {
  
      public string SaleType { get; set; }
      public double CommissionPercent { get; set; }
      public bool? IsReported { get; set; }
      public int? Level { get; set; }
      public string BasedOn { get; set; }
  
      public Project Project { get; set; }
      public int ProjectId { get; set; }
  
      public Employee SalesPerson { get; set; }
      public int SalesPersonEmployeeId { get; set; }
  
      public Employee Consultant { get; set; }
      public int ConsultantEmployeeId { get; set; }
  
  }
  </code></pre>
  
  <p>Here is my DTO:</p>
  
  <pre><code>public class ConsultantGridViewModel
  {
      public string ConsultantName { get; set; }
      public string CustomerName { get; set; }
      public string SalesPersonName { get; set; }
      public string ProjectDescription { get; set; }
      public double? PayRate { get; set; }
      public double? LoadedRated { get; set; }
      public double? BillRate { get; set; }
      public double? GM { get; set; }
      public DateTime? StartDate { get; set; }
      public DateTime? EndDate { get; set; }
      public double CommissionPercent { get; set; }
      public int? CommissionLevel { get; set; }
  
  }
  </code></pre>
  
  <p>Here is my call to EF:</p>
  
  <pre><code>        return await _dbContext.Employee
              .AsNoTracking()
              .Include(e =&gt; e.CommissionDetailConsultants)
                  .ThenInclude(cd =&gt; cd.SalesPerson)
              .Include(e =&gt; e.CommissionDetailConsultants)
                  .ThenInclude(cd =&gt; cd.Project)
                      .ThenInclude(p =&gt; p.Customer)
              .Where(e =&gt; e.EmployeeType == "Contractor")
              .ToListAsync();
  </code></pre>
  
  <p>I'm currently flattening it with SelectMany as follows:</p>
  
  <pre><code>var consultants = employees.SelectMany(e =&gt;
      e.CommissionDetailConsultants,
      (emp, com) =&gt; new ConsultantGridViewModel {
          ConsultantName = emp.Name,
          PayRate = emp.PayRateRegular,
          LoadedRated = emp.PayRateLoadedRegular,
          GM = emp.GMOutput,
          BillRate = com.Project.BillRateRegular,
          ProjectDescription = com.Project.Description,
          ProjectStartDate = com.Project.StartDate,
          ProjectEndDate = com.Project.EndDate,
          CustomerName = com.Project.Customer.Name,
          SalesPersonName = com.SalesPerson.Name,
          CommissionPercent = com.CommissionPercent,
          CommissionLevel = com.Level
      });    
  </code></pre>
  
  <p>I would like to use automapper instead. I've used automapper for all my other DTO mappings but I can't figure out how to use it to flatten a nested object like this.</p>