0

我有两张桌子 PatientEncounterDemoPatientMaster

PatientEncounterDemo

PateientID(F.K int),
DoctorId( F.K int),
ProcedureName



PatientMaster:

PatientId(P.K identity)
PatientFirstName(varchar(50)),
PatientLastName(varchar(50)),
PatientDOB

我需要提取 PatientName(patientFirstName_+patientLastName)、patientDOB 和分配给特定患者的程序计数(患者可以分配许多程序)

我为该结果编写了 SQL 查询:

//This puts the join inside count-

select distinct  PatientFirstName+ ' ' + PatientLastName as PatientName,PatientDOB,COUNT(ProcedureName) as [Proc Count] from
(
select distinct  p.PatientFirstName,p.PatientLastName,p.PatientDOB,e.ProcedureName
from PatientMaster p,PatientEncounterDemo e 
where p.PatientId=e.PatientId and e.DoctorId=1
)as mytable group by PatientFirstName,PatientLastName,PatientDOB

但是,我正在努力通过 linq 查询来做到这一点。请提出一些建议..!!

4

4 回答 4

2

这将是从 SQL 到 LINQ 的(几乎)一对一映射:

var myTable = (
    from encounter in db.PatientEncounterDemos
    where encounter.Doctor.Id == 1
    let patient = encounter.Patient
    select    new 
    {
        patient.FirstName, 
        patient.LastName,         
        patient.DOB,
        encounter.ProcedureName
    })
    .Distinct();

var results =
    from item in myTable
    group item by new { item.FirstName, item.LastName, item.DOB } into g
    select new 
    { 
        PatientName = g.Key.FirstName + " " + g.Key.LastName, 
        g.Key.DOB, 
        ProcCount = g.Count() 
    };

但这会更容易理解:

var myTable = (
    from encounter in db.PatientEncounterDemos
    where encounter.Doctor.Id == 1
    select new { encounter.Patient, encounter.ProcedureName })
    .Distinct();

var results =
    from item in myTable
    group item by item into g
    select new 
    { 
        PatientName =
            g.Key.Patient.FirstName + " " + g.Key.Patient.LastName, 
        g.Key.DOB, 
        ProcCount = g.Count() 
    };
于 2012-10-10T11:29:47.713 回答
1

希望这应该实现你所追求的:

var patientProcedureCounts =
    from master in PatientMasters
    select new
    {
        master.PatientId,
        PatientName = master.PatientFirstName + " " +
            master.PatientLastName,
        master.PatientDOB,
        ProcedureCount = (from demo in PatientEncounterDemos 
            where demo.PatientId == master.PatientId &&
                demo.DoctorId == 1 
            select 1).Count()
    };

这将全选PatientMasters,然后PatientEncounterDemosPatientIdand全选DoctorId。最后,它将使用该患者的主详细信息和演示的计数来构造一个新的匿名类型,可以像这样枚举:

foreach(var procedureInfo in patientProcedureCounts)
{
    Console.WriteLine("Patient {0} born on {1} had {2} procedures", 
        procedureInfo.PatientName, procedureInfo.PatientDOB, procedureInfo.ProcedureCount);
}

以下是 LINQPad 生成的 SQL,看起来与您所追求的非常接近:

-- Region Parameters
DECLARE @p0 NVarChar(1000) SET @p0 = ' '
DECLARE @p1 Int SET @p1 = 1
-- EndRegion
SELECT [t0].[PatientId], ([t0].[PatientFirstName] + @p0) + [t0].[PatientLastName] AS [PatientName], [t0].[PatientDOB], (
    SELECT COUNT(*)
    FROM [PatientEncounterDemo] AS [t1]
    WHERE ([t1].[PatientId] = [t0].[PatientId]) AND ([t1].[DoctorId] = @p1)
    ) AS [ProcedureCount]
FROM [PatientMaster] AS [t0]
于 2012-10-10T11:28:16.483 回答
0

我已经重写了你的 SQL(希望应该可以)。这应该更容易从中生成 LINQ。

SELECT 
    p.PatientFirstName + ' ' + p.PatientLastName as PatientName
    p.PatientDOB,
    COUNT(e.ProcudureName) AS [Proc Count]
FROM 
    PatientMaster p
    INNER JOIN PatientEncounterDemo e ON p.PatientId = e.PatientId
WHERE
    e.DoctorId=1
GROUP BY
    p.PatientFirstName,
    p.PatientLastName,
    p.PatientDOB
于 2012-10-10T11:21:56.797 回答
0

最后,在您所有答案的支持下,我通过这个查询得到了确切的结果。

首先,我与数据库上的 where 条件一起执行内部连接。

然后我按照多个列对结果进行分组,并对该组执行计数。

我的 Linq 查询:

 var myTable = (
          from master in MyDataContext.PatientMasters
          join encounter in MyDataContext.PatientEncounterDemos
          on master.PatientId equals encounter.PatientId
          where encounter.DoctorId == doctorID
          select new { master.PatientFirstName, master.PatientLastName, master.PatientDOB, encounter.ProcedureName })
          .Distinct();

      var results =
          from item in myTable
          group item by new { item.PatientFirstName, item.PatientLastName, item.PatientDOB } into grp
          select new
          {
              PatientName =
                  grp.Key.PatientFirstName + " " + grp.Key.PatientLastName,
              grp.Key.PatientDOB,
              ProcCount = grp.Count()
          };

现在我应用 Foreach 循环将结果添加到列表中..!! 谢谢大家...!!!!

于 2012-10-10T14:30:55.370 回答