Transpose a datatable

后端 未结 3 694
情话喂你
情话喂你 2020-12-22 08:31

I have the data table below. I would like to change the data table to transform the data table into the one next to it. How can I do this?

The reason why i would lik

相关标签:
3条回答
  • 2020-12-22 09:18

    Create a new table, with reversed dimensions from what you previously had, and assign the previous columns to the new rows, and the old rows to the new columns.

    eg:

    oldCol1 -> newRow1
    oldCol2 -> newRow2
    oldCol3 -> newRow3
    oldCol4 -> newRow4
    oldRow1 -> newCol1
    oldCol2 -> newCol2
    oldvar[a][b] -> newvar[b][a]
    

    and the list goes on, assumming that you are going from

    col1 || col2 || col3 || col4
    row1 ||[a][b]||[c][d]||[e][f]
    row2 ||[g][h]||[i][j]||[k][l]
    

    to

    row1 || col1 || col2
    row2 ||[a][b]||[g][h]
    row3 ||[c][d]||[i][j]
    row4 ||[e][f]||[k][l]
    
    0 讨论(0)
  • 2020-12-22 09:28

    .NET does not include a method to transpose data tables. You have to make your own. This website has a tutorial on an example transpose method. I will copy and paste the code snippet below:

    private DataTable Transpose(DataTable dt)
    {
        DataTable dtNew = new DataTable();
    
        //adding columns    
        for(int i=0; i<=dt.Rows.Count; i++)
        {
           dtNew.Columns.Add(i.ToString());
        }
    
    
    
        //Changing Column Captions: 
        dtNew.Columns[0].ColumnName = " ";
    
         for(int i=0; i<dt.Rows.Count; i++) 
         {
          //For dateTime columns use like below
           dtNew.Columns[i+1].ColumnName = Convert.ToDateTime(dt.Rows[i].ItemArray[0].ToString()).ToString("MM/dd/yyyy");
          //Else just assign the ItermArry[0] to the columnName prooperty
         }
    
        //Adding Row Data
        for(int k=1; k<dt.Columns.Count; k++)
        {
            DataRow r = dtNew.NewRow();
            r[0] = dt.Columns[k].ToString(); 
            for(int j=1; j<=dt.Rows.Count; j++)
            r[j] = dt.Rows[j-1][k];  
            dtNew.Rows.Add(r);
        }
    
     return dtNew;
    }
    
    0 讨论(0)
  • 2020-12-22 09:28
    DataTable newDt = new DataTable();
    //Select indexes
    var indexes = dt.Rows.Cast<DataRow>().Select(row => dt.Rows.IndexOf(row));
    //Select the columns
    var newCols = indexes.Select(i => "Row" + i);
    //Add columns
    foreach(var newCol in newCols)
    {
      newDt.Add(newCol);
    }
    //Select rows
    var newRows = dt.Rows.Cast<DataColumn>().Select(col =>
                                                      {
                                                        row = newDt.NewRow();
                                                        foreach(int i in indexes)
                                                        {
                                                          row[i] = dt.Rows[i][col.Name];
                                                        }
                                                        return row;
                                                      });
    //Add row to new datatable
    foreach(var row in newRows)
    {
      newDt.Add(row);
    }
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题