通过DataTable 的列Expression实现 SUM Group by 功能
DataTable dtData = new DataTable();
dtData.Columns.Add("deptID");
dtData.Columns.Add("DateFlag");
DataColumn dc = new DataColumn();
dc.ColumnName = "value1";
dc.DataType = typeof(decimal);
dtData.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "value2";
dc.DataType = typeof(decimal);
dtData.Columns.Add(dc);
DataRow dr;
dr = dtData.NewRow();
dr[0] = "1";
dr[1] = "0901";
dr[2] = "1000";
dr[3] = "10";
dtData.Rows.Add(dr);
dr = dtData.NewRow();
dr[0] = "1";
dr[1] = "0902";
dr[2] = "145";
dr[3] = "15";
dtData.Rows.Add(dr);
dr = dtData.NewRow();
dr[0] = "1";
dr[1] = "0903";
dr[2] = "1456";
dr[3] = "63";
dtData.Rows.Add(dr);
dr = dtData.NewRow();
dr[0] = "1";
dr[1] = "0904";
dr[2] = "146";
dr[3] = "44";
dtData.Rows.Add(dr);
//////////////////////////
dr = dtData.NewRow();
dr[0] = "1";
dr[1] = "0901";
dr[2] = "450";
dr[3] = "67";
dtData.Rows.Add(dr);
dr = dtData.NewRow();
dr[0] = "1";
dr[1] = "0902";
dr[2] = "1335";
dr[3] = "145";
dtData.Rows.Add(dr);
dr = dtData.NewRow();
dr[0] = "1";
dr[1] = "0903";
dr[2] = "1126";
dr[3] = "43";
dtData.Rows.Add(dr);
dr = dtData.NewRow();
dr[0] = "1";
dr[1] = "0904";
dr[2] = "146";
dr[3] = "424";
dtData.Rows.Add(dr);
/////////////////////////////////////
dr = dtData.NewRow();
dr[0] = "2";
dr[1] = "0901";
dr[2] = "1146";
dr[3] = "4124";
dtData.Rows.Add(dr);
dr = dtData.NewRow();
dr[0] = "2";
dr[1] = "0902";
dr[2] = "116";
dr[3] = "414";
dtData.Rows.Add(dr);
dr = dtData.NewRow();
dr[0] = "2";
dr[1] = "0903";
dr[2] = "1124";
dr[3] = "416";
dtData.Rows.Add(dr);
///////////////////////////////////////////
DataTable dt1 = new DataTable();
dt1.Columns.Add("deptID");
dt1.Columns.Add("DateFlag");
dc = new DataColumn();
dc.ColumnName = "value";
dc.DataType = typeof(decimal);
dt1.Columns.Add(dc);
dr = dt1.NewRow();
dr[0] = "1";
dr[1] = "0901";
dt1.Rows.Add(dr);
dr = dt1.NewRow();
dr[0] = "1";
dr[1] = "0902";
dt1.Rows.Add(dr);
dr = dt1.NewRow();
dr[0] = "1";
dr[1] = "0903";
dt1.Rows.Add(dr);
dr = dt1.NewRow();
dr[0] = "1";
dr[1] = "0904";
dt1.Rows.Add(dr);
dr = dt1.NewRow();
dr[0] = "2";
dr[1] = "0901";
dt1.Rows.Add(dr);
dr = dt1.NewRow();
dr[0] = "2";
dr[1] = "0902";
dt1.Rows.Add(dr);
dr = dt1.NewRow();
dr[0] = "2";
dr[1] = "0903";
dt1.Rows.Add(dr);
dr = dt1.NewRow();
dr[0] = "2";
dr[1] = "0904";
dt1.Rows.Add(dr);
//////////////////////////////////
DataSet DataSet1 = new DataSet();
DataSet1.Tables.Add(dtData);
DataSet1.Tables.Add(dt1);
DataColumn[] parent = new DataColumn[2];
parent[0] = dt1.Columns["deptID"];
parent[1] = dt1.Columns["DateFlag"];
DataColumn[] child = new DataColumn[2];
child[0] = dtData.Columns["deptID"];
child[1] = dtData.Columns["DateFlag"];
DataRelation deptDateFlagRel = new DataRelation("deptDateFlagRel", parent, child);
DataSet1.Relations.Add(deptDateFlagRel);
dt1.Columns[2].Expression = "Sum(Child.Value1)";
此时, dt1的Column[2]里面的数据就是根据前面的deptID 和dateFlag合计的dtData的值了.
具体细节请查询MSDN 的DataRelation 和Expression 的例子和说明.