【问题标题】:Get all values related to id (ASP.NET MVC)获取与 id 相关的所有值 (ASP.NET MVC)
【发布时间】:2017-11-15 11:35:43
【问题描述】:

我有两张表患者和约会。

这里是患者模型

 public partial class Patient
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Patient()
    {
        this.Appointments = new HashSet<Appointment>();
        this.Patient_to_investigation = new HashSet<Patient_to_investigation>();
        this.Patient_to_appointment = new HashSet<Patient_to_appointment>();
    }
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Sex { get; set; }
    public string Date_of_Birthday { get; set; }
    public string Adress { get; set; }
    public string Kind_of_medical_insurance { get; set; }
    public string Name_of_medical_insurance { get; set; }
    public string Patients_med_insurance { get; set; }
    public string Status_of_medical_insurance { get; set; }
    public string Index { get; set; }
    public string Country_code { get; set; }
    public string Phone { get; set; }
    public string Refering_organization { get; set; }
    public string Refering_health_center { get; set; }
    public string Refering_doctor { get; set; }
    public string Refering_organization_adress { get; set; }
    public string Additional_fuse { get; set; }
    public Nullable<System.DateTime> Last_edit { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Appointment> Appointments { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Patient_to_investigation> Patient_to_investigation { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Patient_to_appointment> Patient_to_appointment { get; set; }
}

这里是约会模型

 public partial class Appointment
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Appointment()
    {
        this.Patient_to_appointment = new HashSet<Patient_to_appointment>();
        this.Appointments_to_Doctors = new HashSet<Appointments_to_Doctors>();
    }

    [Key]
    public int Id { get; set; }
    public string Start_appointment { get; set; }
    public string End_appointment { get; set; }
    public string Title { get; set; }
    public string Type_of_appointment { get; set; }
    [ForeignKey("Patient")]
    public Nullable<int> Patient_id { get; set; }
    public string Kasse { get; set; }
    public Nullable<System.DateTime> Date { get; set; }

    public virtual Patient Patient { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Patient_to_appointment> Patient_to_appointment { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Appointments_to_Doctors> Appointments_to_Doctors { get; set; }
}

我还有表格可以将患者与预约联系起来(因为一名患者可能有多个预约)

public partial class Patient_to_appointment
{
    [Key]
    public int Id { get; set; }
    public int Patient_id { get; set; }
    public Nullable<int> Appointment_id { get; set; }
    [ForeignKey("Appointment_id")]
    public virtual Appointment Appointment { get; set; }
    [ForeignKey("Patient_id")]
    public virtual Patient Patient { get; set; }
}

在视图中,我有一个按钮,点击获取患者姓名并获取该患者的 ID。

这是我如何在后端执行此操作的代码

  var id = ctx.Patients.Where(x => x.Name == name).Select(x=> new
            {
                id = x.Id
            });

在此之后,我需要获取此患者 ID 的所有预约标题。

如何正确编写查询?

【问题讨论】:

  • 您不需要Patient_to_appointment 表,因为预约已经有患者ID。这意味着您只需要ctx.Appointments.Where(apt =&gt; apt.Patient_id == id)
  • 您的Patient 类已经包含ICollection&lt;Appointment&gt; Appointments(不清楚您为什么需要Patient_to_appointment),因此当您获得您的患者时,约会集合将被填充
  • 好的。我明白这一点。但我有错误。 Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type '&lt;anonymous type: int id&gt;' to 'int?' RS_Main C:\Users\nemes\Source\Repos\RIS_Project_New\RS_Main\Controllers\PatientDatabaseController.cs 43 Active @DavidG
  • 那是哪一行代码?
  • var items = ctx.Appointments.Where(apt =&gt; apt.Patient_id = id)@DavidG

标签: c# asp.net asp.net-mvc entity-framework


【解决方案1】:

请注意,“id”将是可枚举类型。

所以你可以写

List<string> appointmentTitles = new List<string>();
foreach (int patientId in id)
{
    var patientAppointment = ctx.Appointments.Where(x => x.Patient_id == patientId);
    if (patientAppointment != null && patientAppointment.Any())
    {
    // note there can be multiple appointments
        foreach (var appointment from patientAppointment)
        {
            appointmentTitles.Add(appointment.Title)
        }
    }
}

【讨论】:

  • 这会产生多个数据库请求,效率极低。
  • 附带说明,if (patientAppointment != null &amp;&amp; patientAppointment.Any()) 是不必要的(patientAppointment 不能是 null - 只是一个空集合,如果没有项目,则循环不会执行)
【解决方案2】:

为了避免cmets中提到的错误

严重性代码描述项目文件行抑制状态错误 CS0029 无法将类型 '' 隐式转换为 “知道吗?” RS_Main C:\Users\nemes\Source\Repos\RIS_Project_New\R‌​S_Main\Controllers\P‌​atientDatabaseContro‌​ller.cs 43 活跃

您需要删除选择中的新 {}

 var id = ctx.Patients.Where(x => x.Name == name).Select(x=> x.Id);

使用新的 { ...} 您返回的是匿名类型而不是 int。

【讨论】:

    【解决方案3】:

    你可以试试这个。

    var patient = ctx.Patients.Include(a => a.Appointments).FirstOrDefault(x => x.Id == patientId);
    

    这将为您提供患者的数据及其约会,您可以通过迭代约会列表(使用 patient.Appointments)在视图中访问这些数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 2012-06-08
      • 2015-09-02
      • 2021-11-30
      • 1970-01-01
      • 2016-03-02
      相关资源
      最近更新 更多