我创建了一个名为Hospital的简单数据库,我有几列。我已经填写了第一个名为drdoctors的下拉列表。它可以正常工作
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
fetchDoctors();
}
}
void fetchDoctors() {
HospitalEntities entitiy = new HospitalEntities();
List<Doctor> doc = entitiy.Doctors.ToList();
drDoctor.DataSource = doc;
drDoctor.DataTextField = "Name";
drDoctor.DataValueField = "DoctorNo";
drDoctor.DataBind();
}
我想做的就是填写医生病人的另一份记录。
protected void drDoctor_SelectedIndexChanged(object sender, EventArgs e)
{
int id= Int32.Parse( drDoctor.SelectedValue);
HospitalEntities entities = new HospitalEntities();
var query= from p in entities.Doctors
}
但是linq查询非常复杂。我怎么能这样做
答案 0 :(得分:1)
应该这样做。请注意,此代码未经过测试,可能包含轻微错误。
protected void drDoctor_SelectedIndexChanged(object sender, EventArgs e)
{
int id= Int32.Parse( drDoctor.SelectedValue);
HospitalEntities entities = new HospitalEntities();
var query= (from d in entities.Doctors
join m in entities.MedExams on d.DoctorNo equals p.DoctorNo
join p in entities.Patients on m.PatientNo equals p.PatientNo
where d.DoctorNo == id
select p).ToList();
//Populate Patients from query
}
答案 1 :(得分:0)
查看图表,看起来您没有设置外键关系。我强烈建议这样做(出于无数原因)。但通过这样做,您将能够更加轻松地“加入”表格:
protected void drDoctor_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Int32.Parse(drDoctor.SelectedValue);
HospitalEntities entities = new HospitalEntities();
drDoctor.DataSource = entities.Doctors
.Where(x => x.DoctorNo == id)
.SelectMany(x => s.MedExams.Select(y => y.Patients));
drDoctor.DataBind();
}