title: Flatten LINQ/AutoMapper ProjectTo nested children
body: <p>I have reviewed quite a few SO articles related to this question and have used a few of those questions/answers to get close to an answer for my situation, but I can't quite get it right.</p>
  
  <p>I have an EF6 context with 5 tables (Grandparent, GrandparentParent, Parent, ParentChild, and Child).  The GP and PC tables are simple many-to-many relationships that tie the family hierarchy together.</p>
  
  <p>My business requirement is to query database and return a list of grandparent objects that contain a list of all their grandchildren without having the grandchildren nested under a parent.  I have 3 ViewModel classes that are subsets of their corresponding tables and I want to flatten the database hierarchy down to a list of GrandparentViewModel classes where GVM = </p>
  
  <pre><code>public class GrandparentViewModel
  {
      public int GrandparentId { get; set; }
      public string Name { get; set; }
  
      public List&lt;ChildViewModel&gt; Grandchildren { get; set; }
  }
  </code></pre>
  
  <p>It is also important to note that I am using AutoMapper's ProjectTo&lt;> extension method in the query so I can offload the projection and only select a subset of fields from each table....ie...I have DateOfBirth on each table and I do not want to query and/or return that field.</p>
  
  <p>In my attempt to get this working, I stumbled onto AutoMapper's ITypeConverter interface and built a class that implements that interface for the grandparent and for the child.  Here are those classes:</p>
  
  <pre><code>public class GrandparentConverter : ITypeConverter&lt;Grandparent, GrandparentViewModel&gt;
  {
      public GrandparentViewModel Convert(ResolutionContext context)
      {
          var entities = context.SourceValue as Grandparent;
  
          return entities
              .Select(x =&gt; new GrandparentViewModel
              {
                  GrandparentId = x.GrandparentId,
                  Name = x.Name,
                  Grandchildren = AutoMapper.Mapper.Map&lt;IEnumerable&lt;Parent&gt;, List&lt;ChildViewModel&gt;&gt;(x.Parents)
              }).ToList();
  
          //return new GrandparentViewModel
          //{
          //    GrandparentId = entities.GrandparentId,
          //    Name = entities.Name,
          //    Grandchildren = entities.Parents.SelectMany(x =&gt; x.Children.Select(y =&gt; new ChildViewModel
          //    {
          //        ChildId = y.ChildId,
          //        Name = y.Name
          //    }).ToList()).ToList()
          //};
      }
  }
  
  
  public class ChildConverter : ITypeConverter&lt;IEnumerable&lt;Parent&gt;, List&lt;ChildViewModel&gt;&gt;
  {
      public List&lt;ChildViewModel&gt; Convert(ResolutionContext context)
      {
          var entities = context.SourceValue as IEnumerable&lt;Parent&gt;;
  
          return entities
              .SelectMany(x =&gt; x.Children)
              .Select(x =&gt; new ChildViewModel
              {
                  ChildId = x.ChildId,
                  Name = x.Name
              }).ToList();
      }
  }
  </code></pre>
  
  <p>Here is the consuming code:</p>
  
  <pre><code>MapperConfiguration mapper = new MapperConfiguration(cfg =&gt;
          {
              cfg.CreateMap&lt;Child, ChildViewModel&gt;();
              cfg.CreateMap&lt;IEnumerable&lt;Parent&gt;, List&lt;ChildViewModel&gt;&gt;().ConvertUsing&lt;ChildConverter&gt;();
  
              cfg.CreateMap&lt;Grandparent, GrandparentViewModel&gt;();
              cfg.CreateMap&lt;IEnumerable&lt;Grandparent&gt;, List&lt;GrandparentViewModel&gt;&gt;().ConvertUsing&lt;GrandparentConverter&gt;();
          });
  
          FamilyEntities db = new FamilyEntities();
          List&lt;GrandparentViewModel&gt; entities = db.Set&lt;Grandparent&gt;()
              .Where(x =&gt; x.GrandparentId == 1)
              .ProjectTo&lt;GrandparentViewModel&gt;(mapper)
              .ToList();
  </code></pre>
  
  <p>Currently, the solution builds, runs, and returns a singular GrandparentViewModel class as I would expect, but all of the fields (including those on grandparent...ie...name) are null.</p>
  
  <p>Any ideas on what I am missing and/or why the data is not being populated?   I tried setting a breakpoint in that TypeConverter class, but it never executes even though the only mapping configuration specified in AutoMapper's MapperConfiguration class is using that converter.   If I remove that CreateMap call, AutoMapper then throws its "Missing configuration" error.</p>
  
  <p>Any advice is greatly appreciated.</p>
  
  <p>EDIT:   I built a scaled down version of this issue and when I don't build the AutoMapper MapperConfiguration and pass it to the ProjectTo method, the breakpoint in the custom TypeConverter class is hit.  Therefore, it appears that in my original post that the issue is that AutoMapper isn't actually using the converter that is specified in the ConvertUsing method.</p>