码迷,mamicode.com
首页 > 编程语言 > 详细

c# 多维数组、交错数组(转化为DataTable)

时间:2016-05-27 07:04:09      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace ConsoleApplication31
{
    class Program
    {
        public static Array ReturnArray()
        {
            string[,,] arr = new string[2, 3, 4];
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 3; j++)
                {

                    for (int k = 0; k < 4; k++)
                    {

                        arr[i, j, k] = i + "," + j + "," + k;
                    }

                }

            }
            return arr;

        }
     

        private static Array GetJCSZ()
        {
            //string[][][] arr = new string[2][][];
            //arr[0] = new string[2][];
            //arr[0][0] = new string[3];//3
            //arr[0][1] = new string[4];//4
            //arr[0][0][0] = "0,0,0";
            //arr[0][0][1] = "0,0,1";
            //arr[0][0][2] = "0,0,2";




            //arr[1] = new string[2][];
            //arr[1][0] = new string[3];
            //arr[1][1] = new string[4];
            //arr[1][0][0] = "1,0,0";
            //arr[1][0][1] = "1,0,1";
            //arr[1][0][2] = "1,0,2";

            //return arr;

            string[] arr = new string[3];
            arr[0] = "0";
            arr[2] = "2";
            return arr;
        }
      

        public static void InitColumns(Array arr, ref Dictionary<string, DataColumn> dicCols, ref DataTable table)
        {

            for (int i = 0; i < arr.Length; i++)
            {

                if ((arr as dynamic)[i] is Array)
                {

                    InitColumns((arr as dynamic)[i], ref dicCols, ref table);

                }
                else
                {


                    if (arr.Length >= dicCols.Keys.Count)
                    {
                        dicCols.Clear();
                        for (int ii = 0; ii < arr.Length; ii++)
                        {

                            string colName = Guid.NewGuid().ToString();
                            DataColumn col = new DataColumn(colName);
                            if (!dicCols.ContainsKey(colName))
                            {
                                dicCols.Add(colName, col);

                            }


                        }





                    }


                }

            }



        }

        public static DataTable ArrayConvert2DataTable(Array arr)
        {
            DataTable tmpT = new DataTable();
            Dictionary<string, DataColumn> dicCols = new Dictionary<string, DataColumn>();
            Dictionary<string, DataRow> dicRows = new Dictionary<string, DataRow>();
            //J=交 C=错
            bool isJC = !(arr.GetType().Name.Contains(,));
            //交错数组处理
            if (isJC)
            {
                //交错数组第一个维度的元素个

                DataTable table = new DataTable();
                List<int> dims = new List<int>();
                InitColumns(arr, ref dicCols, ref table);
                foreach (var item in dicCols)
                {
                    table.Columns.Add(item.Value);
                }
                int currRowIndex = 0;
                SearchTable(ref currRowIndex,arr,arr,  ref table);

                return table;

            }
            //多维数组处理
            else
            {

                int rank = arr.Rank;
                int cols = arr.GetLength(rank - 1);

                for (int i = 0; i < cols; i++)
                {
                    DataColumn col = new DataColumn(Guid.NewGuid().ToString());
                    tmpT.Columns.Add(col);
                }

                Dictionary<int, int> dims = new Dictionary<int, int>();
                int currRowIndex = -1;
                Dictionary<int, DataRow> dicRow = new Dictionary<int, DataRow>();
                var iterator = arr.GetEnumerator();
                int count = 0;
                while (iterator.MoveNext())
                {
                    var curr = iterator.Current;
                    if (count % cols == 0)
                    {
                        currRowIndex++;
                        DataRow dr = tmpT.NewRow();
                        tmpT.Rows.Add(dr);
                        dicRow.Add(currRowIndex, dr);
                        dr[0] = curr.ToString();
                        if (count == cols)
                        {
                            count = 0;
                        }

                    }
                    else
                    {
                        tmpT.Rows[currRowIndex][count] = curr.ToString();


                    }
                    count++;


                }










            }
            return tmpT;
        }

        private static void SearchTable(ref int currRowIndex, Array ori, Array curr, ref DataTable table)
        {

            for (int i = 0; i < curr.Length; i++)
            {
                bool isa = (curr as dynamic)[i] is Array;
                if (isa)
                {

                    SearchTable(ref currRowIndex, ori, (curr as dynamic)[i], ref table);

                }
                else
                {
                    if (table.Rows.Count < currRowIndex + 1)
                    {
                        DataRow newRow = table.NewRow();
                        table.Rows.Add(newRow);
                    }

                    try
                    {
                        table.Rows[currRowIndex][i] = (curr as Array).GetValue(i);
                    }
                    catch (Exception)
                    {

                        ;
                    }
                    if (i == curr.Length - 1)
                        currRowIndex++;

                }
                 
            }







        }
        static void Main(string[] args)
        {
            var t1 = ArrayConvert2DataTable(ReturnArray());

            var t2 = ArrayConvert2DataTable(GetJCSZ());
            Console.ReadKey();
        }


    }

}

 

c# 多维数组、交错数组(转化为DataTable)

标签:

原文地址:http://www.cnblogs.com/kexb/p/5533279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!