1

我有一个包含以下数据的数据表。我希望它按代码、名称、区域和月份分组,然后使用 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"),
};
4

2 回答 2

0

试试这样:

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["MONTH"].ToString() == "1" && g["COVCODE"].ToString() == "IP"),
          OP1 = grp.Count(g =>g["MONTH"].ToString() == "1"&& g["COVCODE"].ToString() == "OP"),
          MONTH2 = grp.Count(g => g["MONTH"].ToString() == "2"),
          IP2 = grp.Count(g =>g["MONTH"].ToString() == "2" && g["COVCODE"].ToString() == "IP"),
          OP2 = grp.Count(g =>g["MONTH"].ToString() == "2"&& g["COVCODE"].ToString() == "OP"),
          MONTH3 = grp.Count(g => g["MONTH"].ToString() == "3"),
          IP3 = grp.Count(g =>g["MONTH"].ToString() == "3" && g["COVCODE"].ToString() == "IP"),
          OP3 = grp.Count(g =>g["MONTH"].ToString() == "3" && g["COVCODE"].ToString() == "OP")
    };
于 2013-06-28T04:38:51.160 回答
-2
var queryZakaz = (
        from table in dtZakaz.AsEnumerable() 
        group table by table.Field<decimal>("DOCID") into g //группировка по DOCID
        select new
        {
            DOCID=g.Key,
            SUPPLIER_ID = g.Select(table => table["SUPPLIER_ID"].ToString()).FirstOrDefault(),
            totalCountZakazBEZ_NDS=Convert.ToDecimal( g.Where(table => table.Field<decimal>("TAXID") == 1).Count() ),
            totalCountZakazNDS = Convert.ToDecimal(g.Where(table => table.Field<decimal>("TAXID") == 2).Count()),
            totalSumZakazBEZ_NDS=g.Where(table => table.Field<decimal>("TAXID") == 1).Sum(table => table.Field<decimal>("PRICE1")),
            totalSumZakazNDS = g.Where(table => table.Field<decimal>("TAXID") == 2).Sum(table => table.Field<decimal>("PRICE1"))
        });
于 2013-10-07T08:24:22.643 回答