如何将DataTable拆分成多个数据表c#

时间:2018-06-01 11:28:42

标签: c# datatable

Gender      Age    Category
--------------------------------
Male     |  10     |   2
Female   |  15     |   1
Trans    |  13     |   3
Female   |  10     |   1
Male     |  20     |   2

我有一个具有上述值的数据表。男性CategoryId是2.在上表中总共有2个男性行。基于Category,合并两行并分成一个单独的数据表。

我要求的输出是: -

数据表1

Gender      Age    Category
--------------------------------
Male     |  10     |   2
Male     |  20     |   2

DataTable 2

Gender      Age    Category
--------------------------------
Female   |  15     |   1
Female   |  10     |   1

DataTable 3

Gender      Age    Category
--------------------------------
Trans    |  13     |   3

3 个答案:

答案 0 :(得分:2)

var view = sourceDataTable.DefaultView;

view.RowFilter = "Category = 2";
var maleDataTable = view.ToTable();

view.RowFilter = "Category = 1";
var femaleDataTable = view.ToTable();

view.RowFilter = "Category = 3";
var transDataTable = view.ToTable();

答案 1 :(得分:1)

你走了:

List<DataTable> result = DTHead.AsEnumerable()
            .GroupBy(row => row.Field<DataType>("Category"))
            .Select(g => g.CopyToDataTable())
            .ToList();

有关详细信息,请查看此帖:Split Tables

答案 2 :(得分:1)

见以下内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication48
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Gender", typeof(string));
            dt.Columns.Add("Age", typeof(int));
            dt.Columns.Add("Category", typeof(int));

            dt.Rows.Add(new object[] {"Male", 10, 2});
            dt.Rows.Add(new object[] {"Female", 15, 1});
            dt.Rows.Add(new object[] {"Trans", 13, 3});
            dt.Rows.Add(new object[] {"Female", 10, 1});
            dt.Rows.Add(new object[] {"Male", 20, 2});

            DataTable dt1 = dt.AsEnumerable().Where(x => x.Field<string>("Gender") == "Male").CopyToDataTable();
            DataTable dt2 = dt.AsEnumerable().Where(x => x.Field<string>("Gender") == "Feale").CopyToDataTable();
            DataTable dt3 = dt.AsEnumerable().Where(x => x.Field<string>("Gender") == "Trans").CopyToDataTable();


        }

    }

}

这是一个更通用的解决方案,可以获取列中的每个类型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication48
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Gender", typeof(string));
            dt.Columns.Add("Age", typeof(int));
            dt.Columns.Add("Category", typeof(int));

            dt.Rows.Add(new object[] { "Male", 10, 2 });
            dt.Rows.Add(new object[] { "Female", 15, 1 });
            dt.Rows.Add(new object[] { "Trans", 13, 3 });
            dt.Rows.Add(new object[] { "Female", 10, 1 });
            dt.Rows.Add(new object[] { "Male", 20, 2 });

            //updated code
            string[] rowNames = dt.AsEnumerable().Select(x => x.Field<string>("Gender")).Distinct().ToArray();
            DataSet ds = new DataSet();

            foreach (string gender in rowNames)
            {
                DataTable newDt = dt.AsEnumerable().Where(x => x.Field<string>("Gender") == gender).CopyToDataTable();
                newDt.TableName = gender;
                ds.Tables.Add(newDt);

            }
        }

    }

}