c# - Summarizing duplicate rows in datatable -
i have table , want sum duplicate rows:
|name | n | |name | n | |------+---| |------+---| |leo | 1 | |leo | 3 | |wayne | 1 | |wayne | 2 | |joe | 1 | |joe | 1 | |wayne | 1 | |leo | 1 | |leo | 1 |
i can delete this, how summarize?
arraylist uniquerecords = new arraylist(); arraylist duplicaterecords = new arraylist(); foreach (datarow drow in table.rows) { if (uniquerecords.contains(drow["name"])) duplicaterecords.add(drow); else uniquerecords.add(drow["name"]); } foreach (datarow drow in duplicaterecords) { table.rows.remove(drow); }
this how dictionary. create dictionary "name" datarow object , sum datarows' "n" property:
// create intermediate dictionary group records dictionary<string, datarow> summarizedrecords = new dictionary<string, datarow>(); // iterate on records foreach(datarow drow in table.rows) { // if record in dictionary -> sum "n" value if(summarizedrecords.containskey(drow["name"])) { summarizedrecords[drow["name"]].n += drow["n"]; } else { // otherwise add element summarizedrecords[drow["name"]] = drow; } } // transform dictionary list further usage arraylist<datarow> summarizedlist = summarizedrecords.values.tolist();
i think can done more elegantly (1 line of code) linq. let me think more :)
edit
here linq version, involves creating new datarow objects, may not intention - don't know:
arraylist<datarow> summarizedrecords = table.rows.groupby(row => row["name"]) // line groups records "name" .select(group => { int sum = group.sum(item => item["n"]); // line sums "n"'s of group datarow newrow = new datarow(); // create new datarow object newrow["name"] = group.key; // set "name" (key of group) newrow["n"] = sum; // set "n" sum return newrow; // return new datarow }) .tolist(); // make resulting enumerable list
Comments
Post a Comment