-3

有人可以帮我将此查询转换为 linq 吗?我不知道如何使用联合然后在 linq 中求和。

SELECT Patientname, SUM(A)AS Trec
  FROM (SELECT Pm.Patientname, COUNT(*)AS A
          FROM Facilitycheckinholdorder Fcho
              INNER JOIN Medordertype Mot ON Fcho.Orderseq = Mot.Orderseq
              JOIN Patientmaster Pm ON Mot.Patientseq = Pm.Patientseq
                   AND Fcho.Filleddate BETWEEN '2011-09-01 00:00:00:000' AND '2012-10-16 00:00:00:000'
                   AND Mot.Facilityid = 139
         GROUP BY Pm.Patientname
       UNION ALL
       SELECT Pm.Patientname, COUNT(*)AS A
         FROM Rxqdeliveryholdorder Rdho
              INNER JOIN Medordertype Mot ON Rdho.Orderseq = Mot.Orderseq
              JOIN Patientmaster Pm ON Mot.Patientseq = Pm.Patientseq
                   AND Rdho.Filleddate BETWEEN '2011-09-01 00:00:00:000' AND '2012-10-16 00:00:00:000'
                   AND Mot.Facilityid = 139
         GROUP BY Pm.Patientname
      ) AS Trec
  GROUP BY Patientname;
4

2 回答 2

0

关键点:

  • 您最外层的查询似乎是多余的。
  • 你的名字不是常见的 C# 风格,这使得它们难以阅读。
  • UNION ALL对应Concat,不是Union
  • 尽早让路可以让Concat您简化查询。您可以在 SQL 中执行此操作。

.

var result =
    from order in Queryable.Concat(
        FacilityCheckInHoldOrder.Select(o => new { o.OrderSeq, o.FilledDate }),
        RxqDeliveryHoldOrder    .Select(o => new { o.OrderSeq, o.FilledDate }))
    where order.FilledDate >= new DateTime(2011,  9,  1) &&
          order.FilledDate <  new DateTime(2012, 10, 16)
    join type in MedOrderType.Where(t => t.FacilityID == 139)
        on order.OrderSeq equals type.OrderSeq
    join patient in PatientMaster
        on type.PatientSeq equals patient.PatientSeq
    group by patient.PatientName into grp
    select new
    {
        PatientName = grp.Key,
        Trec = grp.Count()
    };
于 2012-10-22T13:35:21.203 回答
0

事实上,这不是一个真正的问题,但我已经尽力了......

var lst1 =
    from fhco in facilitycheckinholdorder
    join mot in meordertype on mot.orderseq equals fhco.orderseq
    join pm in patientmaster on mot.patientseq equals pm.patientseq
    where 
        fcho.FilledDate >= new DateTime(2011, 9, 1)
        fcho.FilledDate <= new DateTime(2012, 10, 16)
    select pm;
var lst2 =
    from rdho in rxqdeliveryholdorder
    join mot in meordertype on rdho.orderseq equals mot.orderseq
    join pm in patientmaster on moit.patientseq equals pm.patientseq
    where 
        fcho.FilledDate >= new DateTime(2011, 9, 1)
        fcho.FilledDate <= new DateTime(2012, 10, 16)
    select pm;
var res = 
    from item in lst1.Union(lst2)
    select new { Name = item.patientname, Count = lst1.Count() + lst2.Count() };
于 2012-10-22T06:39:34.060 回答