LinQ实现DataTable不定行转列 行列转换

来源:互联网 发布:网络视频图片存放 编辑:程序博客网 时间:2024/06/04 17:40
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="LinqDemo2.aspx.cs" Inherits="LinqDemo2" %>      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">      <html xmlns="http://www.w3.org/1999/xhtml">      <head runat="server">          <title>LinQ实现DataTable不定行转列</title>      </head>      <body>          <form id="form1" runat="server">          <div>              原始表:<br />              <asp:GridView ID="GridView1" runat="server" Width="300px">              </asp:GridView>              <br />              转换以后的表:<br />              <asp:GridView ID="GridView2" runat="server" Width="300px">              </asp:GridView>          </div>          </form>      </body>      </html>  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
    using System;      using System.Collections.Generic;      using System.Linq;      using System.Web;      using System.Data;      using System.Web.UI;      using System.Web.UI.WebControls;      public partial class LinqDemo2 : System.Web.UI.Page      {          protected void Page_Load(object sender, EventArgs e)          {              #region  添加一个表              DataTable _dt = new DataTable();              _dt.Columns.Add(new DataColumn("ID", typeof(int)) { DefaultValue = 0 }); //员工 id              _dt.Columns.Add(new DataColumn("Name", typeof(string)) { DefaultValue = "1" });   //员工名字              _dt.Columns.Add(new DataColumn("Item", typeof(string)) { DefaultValue = "1" });//员工提成规则              _dt.Columns.Add(new DataColumn("ItemAmount", typeof(double)) { DefaultValue = 0 });  //提成钱数              _dt.Rows.Add(1, "小李", "零点提成", 60);              _dt.Rows.Add(1, "小李", "订房提成", 70);              _dt.Rows.Add(2, "小张", "零点提成", 500);              _dt.Rows.Add(2, "小张", "订房提成", 60);              _dt.Rows.Add(2, "小张", "订单提成", 800);              _dt.Rows.Add(3, "小王", "零点提成", 30);              _dt.Rows.Add(3, "小王", "订单提成", 900);              #endregion              //输出原始表              GridView1.DataSource = _dt;              GridView1.DataBind();              //输出行转列以后的表              GridView2.DataSource = ConvertToTable(_dt);              GridView2.DataBind();          }          #region   转换表          static DataTable ConvertToTable(DataTable source)          {              DataTable dt = new DataTable();              //前两列是固定的加上              dt.Columns.Add("ID");              dt.Columns.Add("Name");              //以Item 字段为筛选条件  列转为行  下面有图              var columns = (from x in source.Rows.Cast<DataRow>() select x[2].ToString()).Distinct();              //把 Item 字段 做为新字段添加进去              foreach (var item in columns) dt.Columns.Add(item).DefaultValue = 0;              //   x[1] 是字段 Name 按  Name分组 g 是分组后的信息   g.Key 就是名字  如果不懂就去查一个linq group子句进行分组              var data = from x in source.Rows.Cast<DataRow>()                         group x by x[1] into g                         select new { Key = g.Key.ToString(), Items = g };              data.ToList().ForEach(x =>              {                  //这里用的是一个string 数组 也可以用DataRow根据个人需要用                  string[] array = new string[dt.Columns.Count];                  //array[1]就是存名字的                  array[1] = x.Key;                  //从第二列开始遍历                  for (int i = 2; i < dt.Columns.Count; i++)                  {                      // array[0]  就是 ID                      if (array[0] == null)                          array[0] = x.Items.ToList<DataRow>()[0]["ID"].ToString();                      //array[0] = (from y in x.Items                      //            where y[2].ToString() == dt.Columns[i].ToString()                      //            select y[0].ToString()).SingleOrDefault();                      //array[i]就是 各种提成                      array[i] = (from y in x.Items                                  where y[2].ToString() == dt.Columns[i].ToString()//   y[2] 各种提成名字等于table中列的名字                                  select y[3].ToString()                            //  y[3] 就是我们要找的  ItemAmount 各种提成 的钱数                                 ).SingleOrDefault();                  }                  dt.Rows.Add(array);   //添加到table中              });              return dt;          }          #endregion      }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80

转换前后对照

原创粉丝点击