zoukankan      html  css  js  c++  java
  • LinQ实现DataTable不定行转列 行列转换,有图

    <%@ 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>
    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
    }

  • 相关阅读:
    C/S 与 B/S 区别
    XE2的一些新东西
    Versant数据库实验
    概率算法sherwood算法
    组合数学引论部分习题答案
    概率算法n皇后的LV算法
    概率算法Las Vegas
    小型企业库存管理系统的设计与实现
    概率算法Numerical和Monte Carlo
    我的彩票梦十一运夺金模拟程序
  • 原文地址:https://www.cnblogs.com/kliine/p/9909840.html
Copyright © 2011-2022 走看看