DataTable 根据某列分组取出count数量


 //示例代码
        DataTable ndt = new DataTable();
        ndt.Columns.AddRange(new DataColumn[] { new DataColumn("Typename", typeof(string)),
                                        new DataColumn("count", typeof(int)),
                                        new DataColumn("nowCount", typeof(int)) });
        DataTable dtResult = ds.Tables[0].Clone();
        DataTable dtName = ds.Tables[0].DefaultView.ToTable(true, "Typename");
        for (int i = 0; i < dtName.Rows.Count; i++)
        {
            DataRow[] rows = ds.Tables[0].Select("Typename='" + dtName.Rows[i][0] + "'");
            //temp用来存储筛选出来的数据
            DataTable temp = dtResult.Clone();
            foreach (DataRow row in rows)
            {
                temp.Rows.Add(row.ItemArray);
            }

            DataRow dr = ndt.NewRow();
            dr[0] = dtName.Rows[i][0].ToString();
            dr[1] = temp.Compute("count(Typename)", "");
            dr[2] = 0;
            ndt.Rows.Add(dr);
        }

备注: 

ds.Tables[0]  //需要分组的数据源table


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM