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

Gender      Age    Category

--------------------------------

Male     |  10     |   2

Female   |  15     |   1

Trans    |  13     |   3

Female   |  10     |   1

Male     |  20     |   2

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


我需要的输出是:-


数据表 1


Gender      Age    Category

--------------------------------

Male     |  10     |   2

Male     |  20     |   2

数据表2


Gender      Age    Category

--------------------------------

Female   |  15     |   1

Female   |  10     |   1

数据表3


Gender      Age    Category

--------------------------------

Trans    |  13     |   3


翻翻过去那场雪
浏览 368回答 3
3回答

斯蒂芬大帝

干得好:List<DataTable> result = DTHead.AsEnumerable()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GroupBy(row => row.Field<DataType>("Category"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(g => g.CopyToDataTable())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToList();

慕斯王

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();

元芳怎么了

请参阅以下内容:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;namespace ConsoleApplication48{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable dt = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Gender", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Age", typeof(int));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Category", typeof(int));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(new object[] {"Male", 10, 2});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(new object[] {"Female", 15, 1});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(new object[] {"Trans", 13, 3});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(new object[] {"Female", 10, 1});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(new object[] {"Male", 20, 2});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable dt1 = dt.AsEnumerable().Where(x => x.Field<string>("Gender") == "Male").CopyToDataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable dt2 = dt.AsEnumerable().Where(x => x.Field<string>("Gender") == "Feale").CopyToDataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable dt3 = dt.AsEnumerable().Where(x => x.Field<string>("Gender") == "Trans").CopyToDataTable();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}这是一个更通用的解决方案,可以获取列中的每种类型:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;namespace ConsoleApplication48{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable dt = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Gender", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Age", typeof(int));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Category", typeof(int));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(new object[] { "Male", 10, 2 });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(new object[] { "Female", 15, 1 });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(new object[] { "Trans", 13, 3 });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(new object[] { "Female", 10, 1 });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(new object[] { "Male", 20, 2 });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //updated code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] rowNames = dt.AsEnumerable().Select(x => x.Field<string>("Gender")).Distinct().ToArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataSet ds = new DataSet();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (string gender in rowNames)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable newDt = dt.AsEnumerable().Where(x => x.Field<string>("Gender") == gender).CopyToDataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newDt.TableName = gender;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ds.Tables.Add(newDt);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP