将行转换为列字符串构建器

时间:2016-02-16 11:19:19

标签: c# string pivot

我有一个表格包含在邮件中,如下所示: (我从视图(.cshtml)获取数据)

public ActionResult SendForApproveTimesheet(List<TimesheetMatrix> timesheetmatrixList) {
    int n = 1;
    timsheetMail = timsheetMail + " <table style=\"border-collapse:collapse; text-align:center;\" border=\"1\"><tr><td> Date </td><td> Working Hours </td><td> Task </td><td>Comments</td></tr>"; 
    foreach (TimesheetMatrix matrix in timesheetmatrixList) {           
        if ((timesheetData.Hours != Convert.ToDecimal(0.00)) && (timesheetData.Hours.HasValue == true)) {
            timsheetMail = timsheetMail + " <tr><td> " + timesheetData.Date.ToString("dd/MM/yyyy") + "</td><td>" + timesheetData.Hours + "</td><td>" + task + "</td><td>" + timesheetData.Notes +  "</td></tr>";                   
    }
    n++;
}

我已将此邮件包含在邮件中,如下所示:

 mailObj.Body = "Emp Name : " + EmpName + "<br /><br >" + Environment.NewLine + timsheetMail;

现在它包括邮件如下:

Date        Working Hrs    Task                    Comments
08-02-2016  8            Testing        whole application testd
09-02-2016  8          Development        Client Requirments
10-02-2016  8          Installation      Install the app
11-02-2016  8             Design        New design
12-02-2016  8                 DB            db update

但我想改变如下:

              08-02-2016       09-02-2016   10-02-2016  11-02-2016  12-02-2016
Testing         8       
Development                     8       
Installation                                    8   
Design                                                     8    
DB                                                                      8

使用Pivot改变我的桌子。 我怎样才能做到这一点.. 任何人都可以帮我这样做.. 提前谢谢....

1 个答案:

答案 0 :(得分:0)

我不确定,但您应该尝试以下代码,我认为它应该对您的需求有用。

   StringBuilder output = new StringBuilder();
   foreach (DataRow rows in results.Tables[0].Rows)
   {
       foreach (DataColumn col in results.Tables[0].Columns)
       {
          output.AppendFormat("{0} ", rows[col]);
       }
       output.AppendLine();
   }