将数据表拆分为多个表 [英] Split datatable into multiple tables
问题描述
我有这样的数据表
Id名称ParentId
-------- ------ - --------
1 AA 0
2 BB 1
3 CC2 1
4 DD 1
5 EE 0
6 FF 5
7 GG 5
8 HH 0
9 II 8
我想拆分数据包像这样
Id名称ParentId
-------- -------- --------
2 BB 1
3 CC2 1
4 DD 1
Id名称ParentId
-------- -------- --------
6 FF 5
7 GG 5
Id名称ParentId
-------- -------- --- -----
9 II 8
分割是基于parentId ='0'之间的行...
请帮帮我..
谢谢
I have a Datatable like this
Id Name ParentId
-------- -------- --------
1 AA 0
2 BB 1
3 CC2 1
4 DD 1
5 EE 0
6 FF 5
7 GG 5
8 HH 0
9 II 8
I want to split the datatable Like this
Id Name ParentId
-------- -------- --------
2 BB 1
3 CC2 1
4 DD 1
Id Name ParentId
-------- -------- --------
6 FF 5
7 GG 5
Id Name ParentId
-------- -------- --------
9 II 8
the spliting is based on the rows BETWEEN the parentId ='0'
Help me pls..
Thank you
推荐答案
请原谅大内容但请原谅我想给你一个完整的例子,用最有效的方式来得到你想要的结果。
(我只是使用带有page_load的web表单示例页面创建这个逻辑,让你轻松测试和调整)。
Please excuse the large content but I wanted to give you a complete example with the most efficient way to get the result you wish.
(I just used web forms sample page with page_load as a way to create this logic for you to easily test and adjust).
public static class MockData
{
public static DataTable GetMockData()
{
var dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]{
new DataColumn("Id",typeof(Int32)),
new DataColumn("Name",typeof(String)),
new DataColumn("ParentId",typeof(Int32))
});
dt.Rows.Add(new object[] { 1, "AA", 0 });
dt.Rows.Add(new object[] { 2, "BB", 1 });
dt.Rows.Add(new object[] { 3, "CC", 1 });
dt.Rows.Add(new object[] { 4, "DD", 1 });
dt.Rows.Add(new object[] { 5, "EE", 0 });
dt.Rows.Add(new object[] { 6, "FF", 5 });
dt.Rows.Add(new object[] { 7, "GG", 5 });
dt.Rows.Add(new object[] { 8, "HH", 0 });
dt.Rows.Add(new object[] { 9, "II", 8 });
return dt;
}
}
public partial class DataTableSplit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var dts = MockData.GetMockData().AsEnumerable().GroupBy(x => x.Field<int>("ParentId"));
DataSet ds = new DataSet();
foreach(IGrouping<int,DataRow> dataGroup in dts)
{
var groupRows = dataGroup.ToList(); // Convert goruping item to data rows.
DataTable dt = MockData.GetMockData().Clone(); // Copy the structure of the source table to new table.
ds.Tables.Add(dt);
dt.TableName = dataGroup.Key.ToString(); // Set the table name as parent id to make it easier to locate them.
groupRows.ToList().ForEach(x => dt.Rows.Add(x.ItemArray)); // Add the group rows to parent id specific table.
}
foreach(DataTable dt in ds.Tables)
{
Debug.WriteLine("Table name: " + dt.TableName);
foreach(DataRow dr in dt.Rows)
{
Debug.WriteLine(dr.ItemArray.ToList().Aggregate((current, next)=>current + "," + next));
}
}
}
}
输出如下:
表名:0
1,AA,0
5,EE,0
8,HH,0
表名:1
2,BB,1
3,CC,1
4,DD,1
表名:5
6,FF,5
7,GG,5 >
表名:8
9,II,8
The output is as follows:
Table name: 0
1,AA,0
5,EE,0
8,HH,0
Table name: 1
2,BB,1
3,CC,1
4,DD,1
Table name: 5
6,FF,5
7,GG,5
Table name: 8
9,II,8
您可以像这样查询数据表:
You can query your datatable like this:
var tableWhereParentIdIsOne = MyDataTable.AsEnumerable().Where(r => r.Field<int>("ParentId") == 1).CopyToDataTable();</int>
您可以使用多个位置查询数据表atements,排序等
You can query your datatable using multiple where statements, sorting etc.
DataTable tbl = new DataTable("Data").AsEnumerable()
.Where(r => r.Field<int>("ParentId") == 1) // ParentId == 1
.Where(r => r.Field<int>("Id") > 3) // Id > 3
.Where(r => r.Field<string>("Name").Contains("L")) // Name contains L
.OrderBy(r => r.Field<int>("Id")) // Order by Id
.CopyToDataTable();
看起来非常简单:
创建一个新的输出表。
读取所有输入表,行按行。对于每一行:
- 如果第三个字段是
0
,则创建一个新表(并丢弃读取的行)。- 如果第三个字段不是
0
,请将读取行插入当前输出表。
It looks a pretty simple task:
Create a new output table.
Read all the the input table, row by row. For each row:
- if the third field is
0
then create a new table (and discard the read row).- if the third field is NOT
0
, insert the read row into the current output table.
这篇关于将数据表拆分为多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!