我有一个包含以下数据的数据表。我希望它按代码、名称、区域和月份分组,然后使用 LINQ 获取 covcode = (ip 或 OP) 的每个月的计数。
Code Name Region Month CovCode
6018 Provider - ONE REGION2 1 OP
6018 Provider - ONE REGION2 1 IP
6019 Provider - TWO REGION3 2 OP
6019 Provider - TWO REGION3 2 IP
6020 Provider - THREE REGION4 3 IP
6020 Provider - THREE REGION4 3 IP
6020 Provider - THREE REGION4 3 OP
我想要的结果应该是这样的:
Code Name Region MONTH1 IP OP MONTH2 IP OP MONTH3 IP OP
6018 Provider - ONE REGION2 2 1 1 0 0 0 0 0 0
6019 Provider - TWO REGION3 0 0 0 2 1 1 0 0 0
6020 Provider - THREE REGION4 0 0 0 0 0 0 3 2 1
这是我使用的 linq,它返回不正确的结果。
var hospital =
from hosp in tblClaimsMain.AsEnumerable()
group hosp by hosp["ProviderCode"] into grp
select new
{
ProviderCode = grp.Select(g => g["CODE"].ToString()).FirstOrDefault(),
ProviderName = grp.Select(g => g["NAME"].ToString()).FirstOrDefault(),
Region = grp.Select(g => g["REGION"].ToString()).FirstOrDefault(),
MONTH1 = grp.Select(g => g["MONTH"].ToString() == "1").Count(),
MONTH2 = grp.Select(g => g["MONTH"].ToString() == "2").Count(),
MONTH3 = grp.Select(g => g["MONTH"].ToString() == "3").Count()
};
编辑:
嗨,我现在可以每月获得计数,另一方面,我仍然无法获得每个 covcode 每月的计数。我使用了下面的 linq。
var hospital =
from hosp in tblClaimsMain.AsEnumerable()
group hosp by new{CODE=hosp["CODE"],NAME=hosp["NAME"],REGION=hosp["REGION"]} into grp
select new
{
ProviderCode = grp.Key.CODE,
ProviderName = grp.Key.NAME,
Region = grp.Key.REGION,
MONTH1 = grp.Count(g => g["MONTH"].ToString() == "1"),
IP1 = grp.Count(g => g["COVCODE"].ToString() == "INPATIENT"),
OP1 = grp.Count(g => g["COVCODE"].ToString() != "INPATIENT"),
MONTH2 = grp.Count(g => g["MONTH"].ToString() == "2"),
IP2 = grp.Count(g => g["COVCODE"].ToString() == "INPATIENT"),
OP2 = grp.Count(g => g["COVCODE"].ToString() != "INPATIENT"),
MONTH3 = grp.Count(g => g["MONTH"].ToString() == "3")
IP3 = grp.Count(g => g["COVCODE"].ToString() == "INPATIENT"),
OP3 = grp.Count(g => g["COVCODE"].ToString() != "INPATIENT"),
};