0
我有兩個相關的模型,我使用數據對象傳輸類,所以我從相關模型中添加更多的數據。因爲我正在使用DTO,並且由於某些原因,列表未在輸出中鏈接。我需要做什麼才能將PatParDto
數據加入或插入到PatRegDto
DTO的PatPar
? 我的DTO是如何使用EF連接兩個數據傳輸對象(DTO)? [編輯]
public class PatRegDto
{
public string Action { get; set; }
private Int64 _FileId;
public Int64 FileId
{
get
{
return this._FileId;
}
set
{
this._FileId = value;
}
}
public string FName { get; set; }
public string MName { get; set; }
public string LName { get; set; }
public string fullname
{
get { return FName + " " + MName + " " + LName; }
}
public DateTime Dob { get; set; }
public List<PatParDto> PatPar { get; set; }
}
public class PatParDto
{
public string Action { get; set; }
public long RecId { get; set; }
public long FileId { get; set; }
public long ParFileId { get; set; }
public DateTime SDate { get; set; }
public DateTime? EDate { get; set; }
public DateTime dob { get; set; }
public string FullName { get; set; }
}
我控制器
[HttpGet("{id}")]
public async Task<IActionResult> GetPatReg([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var PatientInfo = await _context.PatReg
.Where(a => a.FileId == id)
.Select(b=> new PatRegDto
{
FileId=b.FileId,
FName=b.FName,
PatPar=b.PatPar.ToList() ////????????
})
.ToListAsync();
var PartnerInfo= await _context.PatPar.Select(m => new PatParDto
{
RecId = m.RecId,
FileId = m.FileId,
ParFileId = m.ParFileId,
SDate = m.SDate,
EDate = m.EDate,
}).ToListAsync();
for (int i = 0; i < PartnerInfo.Count; i++)
{
PartnerInfo[i].FullName = _context.PatReg.Where(a => a.FileId == PartnerInfo[i].ParFileId)
.Select(t => new { t.fullname })
.Single().fullname;
PartnerInfo[i].dob = _context.PatReg.Where(a => a.FileId == PartnerInfo[i].ParFileId)
.Select(t => new { t.Dob })
.Single().Dob;
PartnerInfo[i].Action = "Get";
}
if (PatientInfo == null)
{
return NotFound();
}
var DataRes = new {
sdata = PatientInfo
};
return Ok(DataRes);
}
所需的JSON應該像
{
"sdata": {
"fileId": 1708010001,
"fName": "**",
"mName": "**",
"lName": "**",
"fullname": "***",
"dob": "1984-04-26T00:00:00",
"patPar": [{
"recId": 2,
"fullname": "*****",
"fileId": 1708010001,
"parFileId": 1708010002,
"sDate": "1999-12-12T00:00:00",
"eDate": null,
}, {
"recId": 3,
"fullname": "*****",
"fileId": 1708010001,
"parFileId": 1708010003,
"sDate": "1955-12-14T00:00:00",
"eDate": null,
}]
}
}
你的問題不是很清楚 - 你想'PatPar'與'PatReg'中的'fullname'字段?或者你想獲得所有具有'PatPar'的嵌套集合的'PatReg'? 你描述的第二個,但你的DTO和代碼說,第一個 –
實際上,PatPar從啓動DTO得到全名,現在我想DTO包含在PatReg – JSON
我添加'test'只是爲了演示輸出,我需要它在主'JSON'在'一對多'關係 – JSON