我需要找到所有没有看过病人的医生。我有表格:Doctor、Patient 和 DrPatientXref。我有工作的 SQL 查询,但我不知道如何使它成为 Linq to SQL 查询。
select distinct Doctors.FirstName, Doctors.LastName, Doctors.DoctorId
from Doctors, DrPatientXref
where Doctors.DoctorId Not in
(
select DrPatientXref.DoctorId
from DrPatientXref
where DrPatientXref.PatientId = 23)
这是我对它的破解(这是痛苦的错误):
var results = from d in db.Doctors
from x in db.DrPatientXrefs
where
(d.DoctorId == x.DoctorId && x.PatientId != patientId)
select new {
d.DoctorId, d.FirstName, d.LastName
};
var listDrFullName = new List<DoctorFullName>();
foreach (var dr in results) {
DoctorFullName drFullName = new DoctorFullName();
drFullName.FullName = dr.LastName + ", " + dr.FirstName;
drFullName.DoctorId = dr.DoctorId;
listDrFullName.Add(drFullName);
}
return listDrFullName;
解决方案更改变量“结果”。这里是:
var results = db.Doctors.Except(
(from x in db.DrPatientXrefs
join d in db.Doctors on x.DoctorId equals d.DoctorId
where x.PatientId == patientId // probably not needed...
select d)
).ToList();