c# - Splitting a DataTable into 2 using a column index -


i have stored procedure selects differences between rows in table , returns datatable in following format:

col1_a, col2_a, col3_a, col1_b, col2_b, col3b 

i split datatable 2 separate datatables looks like

tablea

col1 col2 col3 

tableb

col1 col2 col3 

this code gets me column index.

        foreach (datacolumn col in dt.columns)         {             if (!col.columnname.endswith("b"))                 tablebindex += 1;             else                 break;         } 

but here i'm not sure how separate rows 2 datatables. ideas on best way accomplish this?

this should trick , fill both tables in 1 loop:

datacolumn[] acols = table.columns.cast<datacolumn>()     .where(c => c.columnname.endswith("a"))     .select(c => new datacolumn(c.columnname, c.datatype))     .toarray(); datacolumn[] bcols = table.columns.cast<datacolumn>()     .where(c => c.columnname.endswith("b"))     .select(c => new datacolumn(c.columnname, c.datatype))     .toarray();  var tablea = new datatable(); tablea.columns.addrange(acols); var tableb = new datatable(); tableb.columns.addrange(bcols);  foreach (datarow row in table.rows) {      datarow arow = tablea.rows.add();     datarow brow = tableb.rows.add();     foreach (datacolumn acol in acols)         arow.setfield(acol, row[acol.columnname]);     foreach (datacolumn bcol in bcols)         brow.setfield(bcol, row[bcol.columnname]); } 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -