拆分一个DataTable到2个或更多的数据表基于列的值 [英] Split a DataTable into 2 or more DataTables based on Column value
问题描述
我有一个名为DTHead,它具有以下的记录,数据表
I have a DataTable called "DTHead" which has the following records,
MIVID Quantity Value
------ ---------- --------
1 10 3000
1 20 3500
1 15 2000
2 20 3000
2 50 7500
3 25 2000
在这里,我需要根据MIVID上述数据表分割成三个表,如如下:
Here, I need to split the above DataTable into three tables based on the MIVID such as follows;
DTChild1:
MIVID Quantity Value
------- ---------- ---------
1 10 3000
1 20 3500
1 15 2000
DTChild2:
DTChild2:
MIVID Quantity Value
------- ---------- ---------
2 20 3000
2 50 7500
DTChild3:
DTChild3:
MIVID Quantity Value
------- ---------- ---------
3 25 2000
假设,如果该部首DataTable包含4种不同的MIVID装置,则4子数据表应根据MIVID创建。如何做到这一点?
Suppose, if the Header DataTable contains 4 different MIVID means, then 4 Child DataTable should be created based on the MIVID. How to do this?
推荐答案
使用 LINQ到数据表
来组由第一列 GROUPBY
和使用方法 CopyToDataTable 来排列表复制到数据表
Use LINQ to DataTable
to group the first column by GroupBy
, and use method CopyToDataTable to copy list of rows to DataTable
List<DataTable> result = DTHead.AsEnumerable()
.GroupBy(row => row.Field<int>("MIVID"))
.Select(g => g.CopyToDataTable())
.ToList();
然后,你可以得到的结果如你预期的数据表的列表。
Then you can get the result as a list of DataTables as you expected.
这篇关于拆分一个DataTable到2个或更多的数据表基于列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!