患者を診察したことのない医師をすべて見つける必要があります。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;
ソリューションは、変数「results」を変更します。ここにあります:
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();