c# Include List<>使用对象到Json结果
本文关键字:对象 Json 结果 Include List | 更新日期: 2023-09-27 18:13:13
我有这个模型:
议程 public class Agenda
{
[Key]
public int AgendaID { get; set; }
public DateTime data { get; set; }
public List<Agendamento> agentamentos { get; set; }
public string status { get; set; }
}
Agendamento
public class Agendamento
{
[Key]
public int AgendamentoID { get; set; }
public Clientes cliente { get; set; }
public DateTime data { get; set; }
public string botijao { get; set; }
public int quantidade { get; set; }
public string veiculo { get; set; }
public string status { get; set; }
}
我的dao:
AgendaDAO
public class AgendaDAO
{
private ApplicationDbContext db = new ApplicationDbContext();
public List<Agenda> Listar()
{
return db.Agenda.Include(a => a.agentamentos).ToList();
}
}
AgendamentoDAO
public class AgendamentoDAO
{
private ApplicationDbContext db = new ApplicationDbContext();
public List<Agendamento> Listar()
{
return db.Agendamentoes.Include(a => a.cliente).ToList();
}
}
在我的ApiController中,我有这些方法:
public class ApiController : Controller
{
// GET: Api/ListarAgenda
public ActionResult ListarAgenda()
{
AgendaDAO adao = new AgendaDAO();
return Json(adao.Listar(), JsonRequestBehavior.AllowGet);
}
// GET: Api/ListarAgendamento
public ActionResult ListarAgendamento()
{
AgendamentoDAO adao = new AgendamentoDAO();
return Json(adao.Listar(), JsonRequestBehavior.AllowGet);
}
}
My Json results:
Api/ListarAgenda
[
{
"AgendaID": 13,
"data": "/Date(1476327600000)/",
"agentamentos": [],
"status": null
},
{
"AgendaID": 14,
"data": "/Date(1476669600000)/",
"agentamentos": [],
"status": null
}
]
Api/ListarAgendamento
[
{
"AgendamentoID": 72,
"cliente": {
"ClientesID": 1007,
"Nome": "Maria Zilda",
"CPF": "00124794321",
"Telefone": 30198269,
"Estado": "PR",
"Cidade": "Curitiba",
"Rua": "Altevir De Souza Gonçalves",
"Numero": 59,
"Lat": "-25.3955309",
"Long": "-49.2168943"
},
"data": "/Date(1476327600000)/",
"botijao": "P13",
"quantidade": 1,
"veiculo": "VW Saveiro ",
"status": null
}]
我的问题是:我需要看到在我的json结果从Api/ListarAgenda与客户端属性的议程列表。像这样:
[
{
"AgendaID": 13,
"data": "/Date(1476327600000)/",
"agentamentos": [
{
"AgendamentoID": 72,
"cliente": {
"ClientesID": 1007,
"Nome": "Maria Zilda",
"CPF": "00124794321",
"Telefone": 30198269,
"Estado": "PR",
"Cidade": "Curitiba",
"Rua": "Altevir De Souza Gonçalves",
"Numero": 59,
"Lat": "-25.3955309",
"Long": "-49.2168943"
},
"data": "/Date(1476327600000)/",
"botijao": "P13",
"quantidade": 1,
"veiculo": "VW Saveiro ",
"status": null
}],
"status": null
}
]
"Include"不能很好地工作。我该怎么做呢?
您正在使用急切加载对吗?您需要包含来自每个议程加载的客户端