2

私はLINQを初めて使用しますが、SQLクエリをLINQに変換しようとして失敗しました。解決策を教えてください。このための正確なLINQは何ですか。よろしくお願いします。

//クエリ全体の一部

  select distinct p.IdPatient,p.IdDoc
  from patd p (NOLOCK)
     left outer join StatusChange sc (NOLOCK)
        on sc.IdPatient = p.IdPatient
           and sc.IdClinicNumber = 23430 
           and sc.IdStatus = 'A'
           and sc.DateStatusChange > GetDate()
     join TrtTyp t ON p.IdTreatmentType = t.IdTreatmentType 
           and t.TypeModality IN ('H','P')
  Where 
      p.IdType IN ('P','E','M')
      and (IsNull(p.IsInactive,0) in (1,0) or sc.IdStatusChange is not null)
      and Not Exists(
         Select 1
         From   Expire e (NOLOCK)
         Where  e.IdPatient = p.IdPatient
      )
      and p.IdClinicNumber = 23430
4

1 に答える 1

1

まず、クエリをより標準的な形式に書き直す必要があります。実際には結合は必要ありません

select distinct
    p.IdPatient, p.IdDoc
from patd as p
where 
    p.IdClinicNumber = 23430 and
    p.IdType in ('P','E','M') and
    p.IdTreatmentType in
    (
         select tt.IdTreatmentType
         from TrtTyp as tt
         where tt.TypeModality in ('H','P')
    ) and
    (
        isnull(p.IsInactive, 0) in (1,0) or
        p.IdPatient in 
        (
            select sc.IdPatient
            from StatusChange as sc
            where
                sc.IdClinicNumber = p.IdClinicNumber and
                sc.IdStatus = 'A' and
                sc.DateStatusChange > GetDate()
        )
    ) and
    p.IdPatient not in
    (
        select e.IdPatient
        from expire as e
    )

これで、LINQ を作成できます。私はそれをテストするためのあなたのデータを持っていないことに注意してください

var query = 
from p in patds
where
    p.IdClinicNumber == 23430 &&
    (new char[] { 'P', 'E', 'M' }).Contains(p.IdType) &&
    (
        from t in TrtTyps
        where (new char[] { 'H','P' }).Contains(t.TypeModality)
        select t.IdTreatmentType
    ).Contains(p.IdTreatmentType) &&
    (
        (new int[] { 1, 0 }).Contains(p.IsInactive ?? 0) ||
        (
            from sc in StatusChanges
            where
                sc.IdClinicNumber == p.IdClinicNumber &&
                sc.IdStatus == 'A' &&
                sc.DateStatusChange > DateTime.Now
            select sc.IdPatient
        ).Contains(p.IdPatient)
    ) &&
    !(
        from e in Expires
        select e.IdPatient
    ).Contains(p.IdPatient)
select new {p.IdPatient, p.IdDoc};
于 2012-11-21T13:25:10.337 に答える