如何使用 EF 联接两个数据传输对象 (DTO)?[已编辑]



我有两个相关的模型,我正在使用数据对象传输类,因此我从相关模型添加了更多数据。 因为我使用的是 DTO,并且由于某种原因,列表未在输出中链接。 我需要做什么才能将PatParDto数据加入或插入PatRegDtoDTO 的PatPar? 我的 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,
}]
}
}

我对任何改进或建议持开放态度,我按如下方式解决它:

简而言之,解决方案是创建两个DTOs,用自己的数据填充每个,我需要在第二个 DTO 中提供更多信息,所以这个还需要一个步骤来获取额外的数据,最后使用共享值加入两个 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> PartnerInfo { 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);
}
// 1 select Parent Record
var PatientInfo = await _context.PatReg 
.Where(a => a.FileId == id)
.Select(b => new PatRegDto
{
Action = "Get",
FileId = b.FileId,
FName = b.FName,
MName = b.MName,
LName = b.LName,
Dob = b.Dob,
}).ToListAsync();
// 2 select Child Record
var PartnerInfo = await _context.PatPar
.Where(s=>s.FileId==id)
.Select(m => new PatParDto 
{
RecId = m.RecId,
FileId = m.FileId,
ParFileId = m.ParFileId,
SDate = m.SDate,
EDate = m.EDate,
}).ToListAsync();
// 3 Fetch more data (not in the original model)
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";
}
// 4 Join parent and child data 
for (int i = 0; i < PatientInfo.Count; i++)
{
PatientInfo[i].PartnerInfo = PartnerInfo.Where(a => a.FileId == PartnerInfo[i].FileId).ToList();
}

if (PatientInfo == null)
{
return NotFound();
}
var DataRes = new {
sdata = PatientInfo
};
return Ok(DataRes);
}

输出为

{
"sdata": [{
"action": "Get",
"fileId": 1708010001,
"fName": "*****",
"mName": "*****",
"lName": "*****",
"fullname": "*******",
"dob": "1984-04-26T00:00:00",
"dateCreated": "2017-09-06T10:50:16.766162Z",
"partnerInfo": [{
"action": "Get",
"recId": 2,
"fileId": 1708010001,
"parFileId": 1708010002,
"sDate": "1999-12-12T00:00:00",
"eDate": null,
"dob": "1984-04-26T00:00:00",
"fullName": "********"
}, {
"action": "Get",
"recId": 3,
"fileId": 1708010001,
"parFileId": 1708010003,
"sDate": "1955-12-14T00:00:00",
"eDate": null,
"dob": "1984-04-26T00:00:00",
"fullName": "********"
}]
}]
}

最新更新