c# - group by Datatable and combine data -


i have datatable this. enter image description here

if datacolumn "dept." , "section" same. want combine data.

like this.

enter image description here

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

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -