c# - Grouping Row In Datatable -


i have data this..

id 1234-001 1234-002 1234-003 5678-001 7890-001 7890-002 

i holding data in datatable. attempting processing on rows groups based on base number i.e. 1234, 5678, 7890

how can iterate through datatable , hold in new (temp) datatable

1234-001,1234-002, 1234-003 clear temp datatable hold  5678-001 clear temp datatable hold 7890-001,7890-002 

i working on old code base , linq not available. cant come elegant solution. maybe dataviews im not sure?

you don't want use linq prefer elegant solution... unless missing vital in question, linqified code seems let want.

var grouped = d in data     group d d.id.split('-').firstordefault();  foreach(var g in grouped) {     // each group } 

non-linq, non-var answer:

datatable data = new datatable(); data.columns.add("id"); data.columns.add("value");  data.rows.add("1234-001", "row 1"); data.rows.add("1234-002", "row 2"); data.rows.add("1234-003", "row 3"); data.rows.add("5678-001", "row 4"); data.rows.add("7890-001", "row 5"); data.rows.add("7890-002", "row 5");  dictionary<string, list<datarow>> grouped = new dictionary<string, list<datarow>>();  foreach(datarow r in data.select()) {     list<datarow> groupedrows;     string key = r["id"].tostring().split('-')[0];      if(!grouped.trygetvalue(key, out groupedrows)) {         groupedrows = new list<datarow>();         grouped[key] = groupedrows;     }      groupedrows.add(r);  }  foreach(keyvaluepair<string, list<datarow>> g in grouped) {          string groupkey = g.key;      console.writeline(groupkey);     foreach(datarow r in g.value) {         console.writeline("\t{0}", r["value"]);     } } 

i following output, i'm not seeing "it groups first 3 , stops":

1234   row 1   row 2   row 3 5678   row 4 7890   row 5   row 5 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -