【发布时间】: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 => apt.Patient_id == id) -
您的
Patient类已经包含ICollection<Appointment> Appointments(不清楚您为什么需要Patient_to_appointment),因此当您获得您的患者时,约会集合将被填充 -
好的。我明白这一点。但我有错误。
Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type '<anonymous type: int id>' 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 => apt.Patient_id = id)@DavidG
标签: c# asp.net asp.net-mvc entity-framework