代码之家  ›  专栏  ›  技术社区  ›  JSON

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

  •  0
  • JSON  · 技术社区  · 7 年前

    我有两个相关的模型,我使用数据对象传输类,所以我从相关模型中添加了更多数据。由于我使用的是DTO,并且由于某些原因,列表在输出中没有链接。我需要做什么才能加入或插入 PatParDto PatPar 属于 PatRegDto

        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);
            }
    

    {
        "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,
                }]
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   JSON    7 年前

    我愿意接受任何改进或建议,我解决了它如下:

    用文字表示 DTOs

    在代码中

        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": "********"
            }]
        }]
    }