I have these two models,
public class PatReg { public Int64 FileId { get; set; } [Required, Display(Name = "First Name")] public string FName { get; set; } [Required, Display(Name = "Middle Name")] public string MName { get; set; } [Required, Display(Name = "Last Name")] public string LName { get; set; } [Display(Name = "Full Name"), NotMapped] public string fullname { get { return FName + " " + MName + " " + LName; } } [Required, Display(Name = "Date of Birth")] [DataType(DataType.Date)] public DateTime Dob { get; set; } public ICollection<PatPar> PatPar { get; set; } } public class PatPar { [Key] public Int64 RecId { get; set; } [Display(Name = "Patient File Id"), Required] public Int64 FileId { set; get; } [Display(Name = "Partner File Id"), Required] public Int64 ParFileId { set; get; } [Display(Name = "Start Date"), DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true), Required] public DateTime SDate { set; get; } [Display(Name = "End Date"), DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime? EDate { set; get; } }
Their JSON output looks like,
{"sdata": {"fileId": 1708010001,"fName": "Json","mName": "S","lName": "Makenzi","fullname": "Json S Abu Makenzi","dob": "1984-04-26T00:00:00","patPar": [{"recId": 2,"fileId": 1708010001,"parFileId": 1708010002,"sDate": "1999-12-12T00:00:00","eDate": null, }, {"recId": 3,"fileId": 1708010001,"parFileId": 1708010003,"sDate": "1955-12-14T00:00:00","eDate": null, }] } }
I m trying to get the output to include the fullname in PatPar. The property exists in PatReg and can be acquired parFileId - FileId
so I have this DTO
public class PatParDto { 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 string FullName { get; set; } }
and inside my controller I tried all the following but failed,
public async Task<IActionResult> GetPatReg([FromRoute] long id) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var PatientInfo = await _context.PatReg.Where(i => i.FileId == id).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, FullName = _context.PatReg.Where(z=>z.FileId==m.ParFileId).Select(x=>x.fullname).ToString() }).ToListAsync(); if (PatientInfo == null) { return NotFound(); } var DataRes = new { sdata = PatientInfo, }; return Ok(DataRes); }
how can I do it?
my desired JSON should look like
{"sdata": {"fileId": 1708010001,"fName": "Json","mName": "S","lName": "Makenzi","fullname": "Json S Abu Makenzi","dob": "1984-04-26T00:00:00","patPar": [{"recId": 2,"fullname": "Sarah S Maz","fileId": 1708010001,"parFileId": 1708010002,"sDate": "1999-12-12T00:00:00","eDate": null, }, {"recId": 3,"fullname": "Mosi Maz","fileId": 1708010001,"parFileId": 1708010003,"sDate": "1955-12-14T00:00:00","eDate": null, }] } }