c# - group by Datatable and combine data -
if datacolumn "dept." , "section" same. want combine data.
like this.
how ?
i try:
var temp = (from x in worktable.asenumerable() group x new { departmentid = x.field<string>("departmentid"), sectionid = x.field<string>("sectionid") } g select g ).tolist();
you can use following loop merge rows:
var depsectgroups = worktable.asenumerable() .groupby(row => new {departmentid = row.field<string>("departmentid"), sectionid = row.field<string>("sectionid")}); datatable resulttable = worktable.clone(); foreach (var rowgroup in depsectgroups) { if (rowgroup.count() == 1) resulttable.importrow(rowgroup.first()); else { datarow addedrow = resulttable.rows.add(); foreach (datarow row in rowgroup) { foreach (datacolumn col in row.table.columns) { if (addedrow.isnull(col.columnname) && !row.isnull(col)) addedrow[col.columnname] = row[col]; } } } }
Comments
Post a Comment