title: Querying EF Core one-to-many and further one-to-many relationship
body: <p>I have 3 models, <code>Competition</code>, <code>Team</code> and <code>TeamUser</code>. Each Competition has many teams and each team has many users. I want to use EF Core to bind all 3 together when returning the data to the view, but I can't really bind to users. Thus I have to implement some complex logic at my view which although it works, looks super messy and confusing. I'm sure there's a way to return the data together.</p>
  
  <p><code>Competition</code> model class:</p>
  
  <pre><code>public class Competition
  {
      [Key]
      public int ID { get; set; }
      [Required]
      [Display(Name = "Competition Name")]
      public string CompetitionName { get; set; }
      [Required]
      public string Status { get; set; }
  
      public ICollection&lt;Team&gt; Teams { get; set; }
  }   
  </code></pre>
  
  <p><code>Team</code> model class:</p>
  
  <pre><code>public class Team
  {
      [Key]
      public int TeamID { get; set; }
  
      [Required]
      [DisplayName("Team Name")]
      public string TeamName { get; set; }
  
      [ForeignKey("CompetitionID")]
      public int CompetitionID { get; set; }
  
      public ICollection&lt;TeamUser&gt; TeamUsers { get; set; }
  
  }
  </code></pre>
  
  <p><code>TeamUser</code> model class:</p>
  
  <pre><code>public class TeamUser
  {
      [Key]
      public int TeamUserID { get; set; }
  
      [ForeignKey("TeamId")]
      public int TeamId { get; set; }
      public string UserId { get; set; }
  }
  </code></pre>
  
  <p>This is my controller.</p>
  
  <pre><code>public IActionResult Index()
  {
       var competition = _context.Competitions
           .Include(c =&gt; c.Teams)
           .ToList();
  
       var teamUsers = _context.TeamUsers
           .ToList();
  
       if (competition == null)
       {
           return NotFound();
       }
  
       CompetitionIndexViewModel vm = new CompetitionIndexViewModel();
       vm.Competition = competition;
       vm.TeamUsers = teamUsers;
  
       return View(vm);
  }
  </code></pre>
  
  <p>As you can see it is very messy and I have to resort to using viewmodel to get my data over. Hope there is a simpler solution. Thank you very much!</p>