title: Web API Returning Nested JSON Values
body: <p>I am working with a JSON structure being returned by a web api, but but have a problem.</p>
  
  <p>Say, I have two tables, <code>Teams</code> and <code>Players</code>. They are joined by <code>TeamID</code> (PK on Teams and FK on Players).</p>
  
  <p>I want my API call to return some JSON format similar as below:</p>
  
  <pre><code>[
      {
          TeamId: 1,
          TeamName: 'Chicago Bulls',
          TeamPlayers: [
              {PlayerId: 1, PlayerName: 'Pau Gasol'},
              {PlayerId: 2, PlayerName: 'Derrick Rose'},
              {PlayerId: 3, PlayerName: 'Joakim Noah'},
              {PlayerId: 4, PlayerName: 'Jimmy Butler'},
              {PlayerId: 5, PlayerName: 'Taj Gibson'}]
      },
      {
          TeamId: 2,
          TeamName: 'Cleveland Cavaliers',
          TeamPlayers: [
              {PlayerId: 1, PlayerName: 'Lebron James'},
              {PlayerId: 2, PlayerName: 'Kyrie Irving'},
              {PlayerId: 3, PlayerName: 'Anderson Varejao'},
              {PlayerId: 4, PlayerName: 'Dion Waiters'},
              {PlayerId: 5, PlayerName: 'Shawn Marion'}]
      },
      {
          TeamId: 3,
          TeamName: 'Los Angeles Clippers',
          TeamPlayers: [
              {PlayerId: 1, PlayerName: 'Chris Paul'},
              {PlayerId: 2, PlayerName: 'Blake Griffin'},
              {PlayerId: 3, PlayerName: 'DeAndre Jordan'},
              {PlayerId: 4, PlayerName: 'Jamal Crawford'},
              {PlayerId: 5, PlayerName: 'Matt Barnes'}]
      }
  ]
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.Entity;
  using System.Data.Entity.Infrastructure;
  using System.Linq;
  using System.Net;
  using System.Net.Http;
  using System.Threading.Tasks;
  using System.Web.Http;
  using System.Web.Http.Description;
  using MyApp.Models;
  
  namespace MyApp.Controllers
  {
      public class TeamsController : ApiController
      {
          private DataModel db = new DataModel();
  
          // GET: api/teams
          public IQueryable&lt;TeamsWithPlayers&gt; GetTeamsAndPlayers()
          {
              var query = from x in db.Teams
                          join y in db.Players on x.TeamId equals y.TeamId
                          select
                          {
                              // This is where I need some help...
                          }
          }
      }
  }
  </code></pre>
  
  <p>TeamAndPlayer Class:</p>
  
  <pre><code>namespace MyApp.Models
  {
      public class TeamAndPlayers
      {
          public int TeamId { get; set; }
          public string TeamName { get; set; }
          public Players players { get; set; }
      }
  }
  </code></pre>
  
  <p>Players Class:</p>
  
  <pre><code>namespace MyApp.Models
  {
      public class Players 
      {
          public int TeamId { get; set; }
          public int PlayerId { get; set; }
          public string PlayerName { get; set; }
      }
  }
  </code></pre>
  
  <p>Can someone provide some insight?</p>