2017-09-06 54 views
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, 
      }] 
    } 
} 
+0

你的問題不是很清楚 - 你想'PatPar'與'PatReg'中的'fullname'字段?或者你想獲得所有具有'PatPar'的嵌套集合的'PatReg'? 你描述的第二個,但你的DTO和代碼說,第一個 –

+0

實際上,PatPar從啓動DTO得到全名,現在我想DTO包含在PatReg – JSON

+0

我添加'test'只是爲了演示輸出,我需要它在主'JSON'在'一對多'關係 – JSON

回答

0

我爲開放給任何改進或建議,我解決了這個問題,如下所示:

用詞 解決方案是創建兩個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": "********" 
     }] 
    }] 
} 
相關問題