c# - DataTable Column Values to different Coulmns of DataTable -


i have datatable 2 columns (column 1 - table name , column2 - column name) i.e.

table_name | columns_name |  sales --------------   prod_id  sales ----------------   sales_id  customer ---------   cust_id  customer ---------- cust_name   product   ------------ product_id  product   ------------ product_name 

i restructure (either using existing datatable or creating new datatable) data adding each tablename , relevant field in different column i.e.

table_name1 | columns_name1 |     table_name2 | columns_name2 |    table_name3 | columns_name3 |  sales --------------   prod_id   | -----|customer ---------   cust_id  | ------- |  product   ------------ product_id    sales ----------------   sales_id |------ |customer ---------   cust_name  | ------ |   product   -------- product_name   

the main datatable filled dynamically different tablenames , columnnames. please right sample code/ loop, take each table relevant field , put them sequentially in different columns.

class program {     static void main(string[] args)     {         datatable table = new datatable();         table.columns.add("table_name", typeof(string));         table.columns.add("column_name", typeof(string));          table.rows.add("sales   ", "prod_id     ");         table.rows.add("sales   ", "sales_id    ");         table.rows.add("customer", "cust_id     ");         table.rows.add("customer", "cust_name   ");         table.rows.add("product ", "product_id  ");         table.rows.add("product ", "product_name");          datatable newtable = rotatedatatable(table, 0);          foreach (datarow datarow in newtable.rows)         {             foreach (var item in datarow.itemarray)             {                 console.write("#" + item + "# ");             }             console.writeline();         }     }      private static datatable rotatedatatable(datatable table, int groupingcolumn)     {          var grouped = table.asenumerable()                 .groupby(dr => dr.itemarray[groupingcolumn])                 .select(grouping => grouping.tolist())                 .tolist();         int count = grouped.count;         int rowcount = grouped[0].count;         if (!grouped.all(g => g.count == rowcount))             throw new exception("unexpected input");           datatable newtable = new datatable();          (int = 0; < count; i++)         {             (int j = 0; j < table.columns.count; j++)                 newtable.columns.add(table.columns[j].columnname + (i + j));         }          (int = 0; < rowcount; i++)             newtable.rows.add(newtable.newrow());          (int = 0; < count; i++)         {             var list = grouped[i].tolist();             (int j = 0; j < rowcount; j++)             {                 (int k = 0; k < list[j].itemarray.length; k++)                 {                     string field = list[j].field<string>(k);                     int index = (i * table.columns.count) + k;                     newtable.rows[j].setfield<string>(index, field);                 }             }         }         return newtable;     } } 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -