Datatable Linq GroupBy,Aggregate Count

时间:2013-06-28 04:24:17

标签: c# linq datatable group-by aggregate

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

2 个答案:

答案 0 :(得分: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")
    };

答案 1 :(得分:-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"))
                });