title: Entity Framework Map Nested entites to View Model
body: <p>Here are my view models. The entities are exactly the same structure except the name has no ViewModel</p>
  
  <pre><code>public class CelulaViewModel
  {
      public int Id { get; set; }
      public string Nome { get; set; }
      public int? CheckListId { get; set; }
      public CheckList CheckList { get; set; }
  }
  
  public class CheckListViewModel
  {
      public int Id { get; set; }
      public string Nome { get; set; }        
      public IList&lt;CheckListGrupoViewModel&gt; Grupos { get; set; }
  
      public IList&lt;CelulaViewModel&gt; Celulas { get; set; }
  }
  
  public class CheckListGrupoViewModel
  {
      public int Id { get; set; }
      public string Nome { get; set; }
      public IList&lt;CheckListGrupoItemViewModel&gt; Items { get; set; }
  }
  
  public class CheckListGrupoItemViewModel
  {
      public int Id { get; set; }
      public string Nome { get; set; }
  
      public string Value { get; set; }
  }
  </code></pre>
  
  <p>Here is my page Model</p>
  
  <pre><code>    public class IndexModel : PageModel
      {
          private readonly DatabaseContext _context;
  
          public IndexModel(DatabaseContext context)
          {
              _context = context;
          }
  
          #region Properties    
          public CelulaViewModel Celula { get; set; }
  
  
          #endregion
  
          #region Handlers
          public IActionResult OnGet(int? id)
          {
              if (id == null)
              {
                  return NotFound();
              }
  
              Celula = _context.Celulas
                  .Include(c =&gt; c.CheckList)
                  .ThenInclude(cl =&gt; cl.Grupos)
                  .ThenInclude(cl =&gt; cl.Items)
                  .Where(c =&gt; c.Automated)
                  .Select(c =&gt; new CelulaViewModel
                  {
                      Id = c.Id,
                      Nome = c.Nome,
                      CheckListId = c.CheckListId,
                      CheckList = new CheckListViewModel
                      {
                          Id = c.CheckList.Id,
                          Nome = c.CheckList.Nome,
                          Grupos = new List&lt;CheckListGrupoViewModel&gt;
                          {
                             // ?? How to map the list in here?
                          }
                      }
                  })
                  .FirstOrDefault(c =&gt; c.Id == id);
  
              if (Celula == null)
              {
                  return NotFound();
              }
  
              return Page();
          }
  
          public IActionResult OnPost(string[] id)
          {
              return RedirectToPage("../Index");
          }
          #endregion
      }
  </code></pre>
  
  <p>As you can see i'm trying to map the enty and sub entities to the view model and sub view models. The problem is when trying to do it with ICollections. I have no idea how to deal with this in lambda expression</p>