我有一個數據表,其中包含下面的數據。我希望它通過代碼,名稱,地區和月份分組 然後獲得使用LINQ的covcode =(ip或OP)每月的計數。Datatable Linq GroupBy,Aggregate Count
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"),
};
嗨,蒂姆,你的回覆幫助了我很多,但考慮到上面的示例,當我嘗試獲得每月計數和covcode(IP,OP)結果是不正確的。 – Vhinchies
更新我的代碼,希望它能給你幫助:) –
感謝迅速回復tim。我現在就試一試。 – Vhinchies