按列计数拆分数据表

时间:2015-03-13 16:25:24

标签: c# datatable split

如何拆分具有动态列数和名称

的DataTable

理想情况下,我喜欢像

这样的方法签名
List<DataTable> SplitColumnOnColumn(DataTable table, int count)

| columnFirst | columnSecond | columnThird | columnFourth | columnFifth |
| row1val1 | row1val2 | row1val3 | row1val4 | row1val5 |
| row2val1 | row2val2 | row2val3 | row2val4 | row2val5 |
| row3val1 | row3val2 | row3val3 | row3val4 | row3val5 |

鉴于上表

SplitColumnOnColumn(myTable, 3)

哪会产生

| columnFirst | columnSecond | columnThird |
| row1val1 | row1val2 | row1val3 |
| row2val1 | row2val2 | row2val3 |
| row3val1 | row3val2 | row3val3 |

| columnFourth | columnFifth |
| row1val4 | row1val5 |
| row2val4 | row2val5 |
| row3val4 | row3val5 |

2 个答案:

答案 0 :(得分:0)

由于DataTable不提供开箱即用的此功能,您必须自己创建。

我的建议是克隆你的DataTable,然后从每个表中删除你不想要的列。

将以下示例修改为可用的函数留给读者。

static void Main(string[] args)
    {
        int[] sourceData = {0,0,0,0,0};
        DataTable source = new DataTable();
        source.Columns.Add("ColOne");
        source.Columns.Add("ColTwo");
        source.Columns.Add("ColThree");
        source.Columns.Add("ColFour");
        source.Columns.Add("ColFive");

        for (var rowIndex = 0; rowIndex < 5; rowIndex++)
        {
            for (var colIndex = 0; colIndex < source.Columns.Count; colIndex++)
            {
                sourceData[colIndex] = rowIndex * colIndex;
            }
            source.Rows.Add(sourceData);
        }

        DataTable target = new DataTable();
        target = source.Clone();

        Console.WriteLine("Before split");
        Console.Write("Source -> ");
        foreach (DataColumn col in source.Columns) { 
            Console.Write(col.ColumnName + ", ");
        }
        Console.WriteLine();
        Console.Write("Target -> ");
        foreach (DataColumn col in target.Columns)
        {
            Console.Write(col.ColumnName + ", ");
        }
        Console.WriteLine();

        target.Columns.RemoveAt(0);
        target.Columns.RemoveAt(0);
        target.Columns.RemoveAt(0);

        source.Columns.RemoveAt(3);
        source.Columns.RemoveAt(3);

        Console.WriteLine();
        Console.WriteLine("After split");
        Console.Write("Source -> ");
        foreach (DataColumn col in source.Columns)
        {
            Console.Write(col.ColumnName + ", ");
        }
        Console.WriteLine();
        Console.Write("Target -> ");
        foreach (DataColumn col in target.Columns)
        {
            Console.Write(col.ColumnName + ", ");
        }
        Console.WriteLine();

        if (Debugger.IsAttached) {
            Console.WriteLine();
            Console.WriteLine("ANY KEY TO EXIT");
            Console.ReadKey(true);
        }
    }

答案 1 :(得分:0)

在Dan-o关于如何解决这个问题的建议之后,我提出了这个问题。

public List<DataTable> SplitOnColumnCount(DataTable inputTable, int columnCount)
    {
        if (inputTable == null || columnCount < 1)
        {
            return null;
        }

        var tableList = new List<DataTable>();
        var grouping = Convert.ToInt32(Math.Ceiling((inputTable.Columns.Count) / ((double)columnCount)));
        var columnNames = inputTable.Columns.Cast<DataColumn>().Select(_ => _.ColumnName).ToArray();

        for (var i = 1; i <= grouping; i++)
        {
            var columnsThisTime = columnNames.Skip((i - 1) * columnCount).Take(columnCount).ToArray();
            var newTable = inputTable.Copy();
            var removingColumns = columnNames.Where(_ => columnsThisTime.All(keep => keep != _));
            foreach (var columnName in removingColumns)
            {
                newTable.Columns.Remove(columnName);
            }
            newTable.AcceptChanges();
            tableList.Add(newTable);
        }

        return tableList;
    }