我有两张桌子
PatientEncounterDemo
和PatientMaster
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查询来做到这一点。请建议某种方式.. !!
答案 0 :(得分: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()
};
答案 1 :(得分: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
,然后为PatientEncounterDemos
和PatientId
选择所有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]
答案 2 :(得分: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
答案 3 :(得分:0)
最后,在你所有答案的支持下,我得到了这个查询的确切结果。
首先,我执行了内连接以及db上的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循环将结果导入列表.. !! 感谢所有...... !!!!