0

患者を診察したことのない医師をすべて見つける必要があります。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();
4

2 に答える 2

0

Linq でこれと他の同様のクエリを実行する最も簡単な方法は、Linq のセット ベースの操作( ExceptIntersectUnionDistinct) を利用することです。以下は、Exceptクエリ内で使用する例です。

var drdrs = db.Doctors.Except(
  (from x in db.DrPatientXrefs
  join d in db.Doctors on d.DoctorId equals x.DoctorId
  where x.PatientId == patientId // probably not needed...
  select d)
).ToList();

これで(テストされていない)トリックが実行され、プロセス内のコードが単純になると思います。

于 2013-10-01T00:15:25.490 に答える