zoukankan      html  css  js  c++  java
  • datatable 行列转换

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable dt = GetDt();
                DataTable res = Col2Row(dt, "Col01");
                DataTable test = Col2Row(dt);
            }
    
    
            public static DataTable Col2Row(DataTable src, int columnHead)
            {
                DataTable result = new DataTable();
                DataColumn myHead = src.Columns[columnHead];
                result.Columns.Add(myHead.ColumnName);
                for (int i = 0; i < src.Rows.Count; i++)
                {
                    result.Columns.Add(src.Rows[i][myHead].ToString());
                }
                //
                foreach (DataColumn col in src.Columns)
                {
                    if (col == myHead)
                        continue;
                    object[] newRow = new object[src.Rows.Count + 1];
                    newRow[0] = col.ColumnName;
                    for (int i = 0; i < src.Rows.Count; i++)
                    {
                        newRow[i + 1] = src.Rows[i][col];
                    }
                    result.Rows.Add(newRow);
                }
                return result;
            }
    
            public static DataTable Col2Row(DataTable src, string columnHead)
            {
                for (int i = 0; i < src.Columns.Count; i++)
                {
                    if (src.Columns[i].ColumnName.ToUpper() == columnHead.ToUpper())
                        return Col2Row(src, i);
                }
                return new DataTable();
            }
    
    
            private static DataTable GetDt()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Col01");
                dt.Columns.Add("Col02");
                dt.Columns.Add("Col03");
    
                DataRow dr = dt.NewRow();
                dr["Col01"] = 1;
                dr["col02"] = 2;
                dr["Col03"] = 3;
                dt.Rows.Add(dr);
    
                DataRow dr2 = dt.NewRow();
                dr2["Col01"] = 4;
                dr2["col02"] = 5;
                dr2["Col03"] = 6;
                dt.Rows.Add(dr2);
    
                DataRow dr3 = dt.NewRow();
                dr3["Col01"] = 7;
                dr3["col02"] = 8;
                dr3["Col03"] = 9;
                dt.Rows.Add(dr3);
    
                return dt;
            }
    
            private static DataTable Col2Row(DataTable dt)
            {
                DataTable result = new DataTable();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    result.Columns.Add(i.ToString());
                }
                result.Columns.Add(dt.Rows.Count.ToString());
                foreach (DataColumn col in dt.Columns)
                {
                    object[] newRow = new object[dt.Rows.Count + 1];
                    newRow[0] = col.ColumnName;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        newRow[i + 1] = dt.Rows[i][col];
                    }
                    result.Rows.Add(newRow);
                }            
                return result;
            }
    
        }
    }

    关于行列转换,记下来,免得以后要用在写。

  • 相关阅读:
    『Argparse』命令行解析
    『MXNet』专题汇总
    用.NET开发通用Windows App
    ASP.NET 5探险(6):升级ASP.NET 5到beta6
    使用ASP.NET MVC、Rabbit WeixinSDK和Azure快速开发部署微信后台
    Visual Studio 2015将在7月20号RTM
    VS2015上又一必备免费插件:Refactoring Essentials
    ASP.NET 5探险(5):利用AzureAD实现单点登录
    Visual Studio Code升级到0.5,提供对ES6的更好支持
    ASP.NET 5探险(4):如何把ASP.NET 5从beta4升级到beta5
  • 原文地址:https://www.cnblogs.com/wodegui/p/4667724.html
Copyright © 2011-2022 走看看