【问题标题】:Joins with Multiple Table and showing data from multiple tables in the View与多个表连接并在视图中显示来自多个表的数据
【发布时间】:2019-09-06 07:41:33
【问题描述】:

我必须将数据放入下表中:

此表中的数据主要来自表“tblPatient”,但该表中的最后一个条目,即“AppointmentDate”来自另一个表,但要获得这个单个条目,我必须将我的查询与三个表连接起来,即 tbl病人 tblPatientBill 和 tblPatientAppointment。 为此,我在控制器中编写了此查询

public ActionResult PrintPartialViewToPdf(int id)
        {
            List<tblPatientAppointment> tblappointments = db.tblPatientAppointments.ToList();
            List<tblPatientBill> tblbill = db.tblPatientBills.Where(x=>x.ID == id && x.is_active == true).ToList();
            List<tblPatient> tblpatient = db.tblPatients.ToList();


            var result = (from p in tblbill
                          join o in tblappointments on p.PatientAppointmentID equals o.ID
                          join c in tblpatient on o.patient_id equals c.Patient_id
                          select new
                          {
                              c.Patient_Name,
                              c.Patient_address,
                              c.Contact_no,
                              c.Age,
                              c.Gender,
                              c.Date_of_Birth,
                              o.AppointmentDate,
                          }).ToList();


                var report = new PartialViewAsPdf("~/Views/Shared/PatientBillToPDF.cshtml", result);
                return report;


        }

在我看来,当我想在视图中显示结果时,这是我的代码。 PatientBillToPDF.cshtml

@model IEnumerable<HMS.Models.tblPatient>

@{
    Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <style>
        .bottom {
            vertical-align: middle;
        }
    </style>
</head>
<body>
<div class="container">
        <div class="row clearfix">
            <div class="container-fluid">
                <div class="col-lg-12 col-md-12 col-sm-12">
                    <div class="block-header">
                        <h3>Patient's Personal Detail</h3>
                    </div>
                    <div class="card">
                        <div class="body table-responsive">

                            <table class="table table-bordered table-striped table-hover js-basic-example dataTable" style="width:100%">
                                @foreach (var item in Model)
                                {
                                    <tr>
                                        <th>@Html.DisplayNameFor(model => model.Patient_Name)</th>
                                        <td>@Html.DisplayFor(model => item.Patient_Name)</td>
                                    </tr>
                                    <tr>
                                        <th>@Html.DisplayNameFor(model => model.Patient_address)</th>
                                        <td>@Html.DisplayFor(model => item.Patient_address)</td>
                                    </tr>
                                    <tr>
                                        <th>@Html.DisplayNameFor(model => model.Contact_no)</th>
                                        <td>@Html.DisplayFor(model => item.Contact_no)</td>
                                    </tr>
                                    <tr>
                                        <th>@Html.DisplayNameFor(model => model.Age)</th>
                                        <td>@Html.DisplayFor(model => item.Age)</td>
                                    </tr>
                                    <tr>
                                        <th>@Html.DisplayNameFor(model => model.Gender)</th>
                                        <td>@Html.DisplayFor(model => item.Gender)</td>
                                    </tr>
                                    <tr>
                                        <th>@Html.DisplayNameFor(model => model.Date_of_Birth)</th>
                                        <td>@Html.DisplayFor(model => item.Date_of_Birth)</td>
                                    </tr>
                                    <tr>
                                        <th>@Html.DisplayNameFor(model => model.AppointmentDate)</th>
                                        <td>@Html.DisplayFor(model => item.AppointmentDate)</td>
                                    </tr>
                                }
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

我想知道的: 请检查我的 linq 查询是否符合要求?第二件事是在我看来,我无法获得约会日期,因为它包括 tblPatient 模型。如何显示查询返回结果的两个不同表中的数据,但我无法在视图中显示它,因为它们属于两个不同的模型。谢谢

【问题讨论】:

标签: asp.net-mvc entity-framework linq join


【解决方案1】:

如何显示查询返回结果的两个不同表中的数据

您实际上需要在此处创建一个 ViewModel 并使用它强输入您的视图。您的 ViewModel 如下所示:

public class PatientAppointmentViewModel
{
     public string PatientName { get;set; }
     public string PatientAddress { get;set; }
     public string ContactNo { get;set;}
     ......
     ......
     public DateTime AppointmentDate { get;set;}
}

在控制器中,我们需要填充 ViewModel :

 var result = (from p in tblbill
                      join o in tblappointments on p.PatientAppointmentID equals o.ID
                      join c in tblpatient on o.patient_id equals c.Patient_id
                      select new PatientAppointmentViewModel
                      {
                          PatientName = c.Patient_Name,
                          PatientAddress = c.Patient_address,
                          ......
                          ......
                          AppointmentDate = o.AppointmentDate
                      }).ToList();

View 需要绑定IEnumerable&lt;PatientAppointmentViewModel&gt;:

@model IEnumerable<HMS.Models.PatientAppointmentViewModel>

@{
    Layout = null;
}

请检查我的linq查询是否符合要求

您的查询可能是正确的,但一件重要的事情是您将所有数据加载到内存中,然后对其进行 linq 查询,如果数据太大,这将无法在生产中工作。 例如,以下内容将从不正确的 db 加载所有患者预约:

List<tblPatientAppointment> tblappointments = db.tblPatientAppointments.ToList();

这一行之后的其他两行也是如此。

您应该直接在 linq 查询中使用上下文对象,以便它仅在内存中加载符合查询条件的行,例如:

var result = (from p in db.tblPatientBills
              join o in db.tblPatientAppointmentson p.PatientAppointmentID equals o.ID
              join c in db.tblPatients on o.patient_id equals c.Patient_id
              where p.ID == id && p.is_active == true
              select new PatientAppointmentViewModel
              {
                  PatientName = c.Patient_Name,
                  PatientAddress = c.Patient_address,
                  ......
                  ......
                  AppointmentDate = o.AppointmentDate
               }).ToList();

【讨论】:

  • 有道理让我试试看。
  • 非常感谢您的指导(y)
  • 很高兴它有帮助:)
【解决方案2】:

当您显示约会日期时,您的代码似乎不正确。

<tr>
<th>@Html.DisplayNameFor(model => model.AppointmentDate)</th>
<td>@Html.DisplayFor(model => item.Date_of_Birth)</td>
</tr>

第三行将显示出生日期。 将其更改为

<td>@Html.DisplayFor(model => model.AppointmentDate)</td>

要知道查询是否有效,您必须解释您要执行的操作。您是否获得单个患者的结果。如果我是你,我也不会在运行查询之前将整个表加载到内存中。

【讨论】:

  • 是的,我正在为单个患者获取结果,并且我在第三行将其写为 AppointmentDate,它只是在这里弄错了。让我编辑它。
  • 实际的问题是我如何在表中检索约会日期,因为我在视图中只添加了一个模型。
猜你喜欢
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多