Bat*_*uta 4 c# sql json asp.net-web-api
我正在使用由web api返回的JSON结构,但是有问题.
说,我有两张桌子,Teams和Players.他们加入了TeamID(PK on Teams and FK on Players).
我希望我的API调用返回一些类似如下的JSON格式:
[
{
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'}]
}
]
Run Code Online (Sandbox Code Playgroud)
控制器:
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<TeamsWithPlayers> 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...
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
TeamAndPlayer类:
namespace MyApp.Models
{
public class TeamAndPlayers
{
public int TeamId { get; set; }
public string TeamName { get; set; }
public Players players { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
球员类:
namespace MyApp.Models
{
public class Players
{
public int TeamId { get; set; }
public int PlayerId { get; set; }
public string PlayerName { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
有人能提供一些见解吗?
Oma*_*ani 11
我将假设JSON结构是真实的来源,而不是模型(TeamWithPlayers/Players) - 请参阅我的评论.
我的解决方案假设您正在使用Entity Framework从数据库中检索数据,因为我使用的是"Include"方法,但可以将其替换为使用"Join".
1-将TeamDto和PlayerDto类定义为:
public class TeamDto
{
public int TeamId { get; set; }
public string TeamName { get; set; }
public IEnumerable<PlayerDto> TeamPlayers { get; set; }
}
public class PlayerDto
{
public int PlayerId { get; set; }
public string PlayerName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
2-你的TeamsController将是这样的:
public class TeamsController : ApiController
{
private readonly TeamDbContext _dbContext = new TeamDbContext();
// GET api/teams
public IEnumerable<TeamDto> GetTeamsAndPlayers()
{
var teams = _dbContext
.Teams
.Include("Players") // Load the players associated with each Team, (this depends on your declaration, but you mentioned that there is a FK from Player => TeamId)
// you can use the join if you like or if you don't use entity framework where you cannot call Include, but the following code will stay the same
.Select(t => new TeamDto
{
TeamId = t.TeamId,
TeamName = t.TeamName,
TeamPlayers = t.Players.Select(p => new PlayerDto
{
PlayerId = p.PlayerId,
PlayerName = p.PlayerName
})
}).ToList();
return teams;
}
}
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.
| 归档时间: |
|
| 查看次数: |
14021 次 |
| 最近记录: |