i have datatable 3 columns ( first name, last name, age), wanna browse through , rows have same first , last name, , replace them 1 row that'll have same first , last name. age column wanna put ages in 1 cell , seperate them '|' or ';'.
before:
john # doe # 24
john # doe # 35
after:
john # doe # 24|35
you can use linq , extensions in system.data.datasetextensions.dll
assembly datarowextensions group rows , join values want this:
var dt=new datatable(); dt.columns.add("a", typeof (string)); dt.columns.add("b", typeof (string)); dt.columns.add("c", typeof (int)); dt.rows.add("john", "doe", 23); dt.rows.add("john", "doe", 24); dt.rows.add("john", "doe", 25); var result = row in dt.asenumerable() group row new {a=row.field<string>("a"), b=row.field<string>("b")} grp select new { customername = grp.key, items = string.join(",",grp.select(r=>r.field<int>("c"))) }; foreach (var t in result) console.writeline(t.customername + " " + t.items);
the trick realize ask how group data , aggregate value column using other count() or sum()
you'll find many questions in ask how group datatable rows, eg. this one can use reference make more complex groupings
Comments
Post a Comment