将数据表数组中的数据列与数据合并 [英] Merging datacolumns from a datatable array with data
问题描述
我有一个数据表数组,
DataSet ds = //我从数据源收集数据
我从ds.Tables初始化 DataTable []
DataTable [] tableArray = ds.Tables.Cast< DataTable>();
这些表格都有一行数据
现在是我可以做的另一种方法,合并这些DataTables的列并放置数据在列中根据需要?
示例数据
让我的 DataSet ds
有2个表 dtA
和 dtB
输入
dtA
Col1 A
Col2 B
dtB
Col3 C
Col4 D
输出
dtMerged
Col1 A
Col2 B
Col3 C
Col4 D
我尝试了什么:
我在列和行上运行了一个简单的
形成一个更大的数据表。 foreach
循环
此外,
I have a datatable array,
DataSet ds = // I gather data from a datasource
I initialize a DataTable[]
from ds.Tables
DataTable[] tableArray = ds.Tables.Cast<DataTable>();
These tables all have a single row of data
Now is an alternate approach to what I have done possible, to merge the columns of these DataTables and also put data in the columns as required ?
Example Data
Let my DataSet ds
have 2 tables dtA
and dtB
Input
dtA
Col1 A
Col2 B
dtB
Col3 C
Col4 D
Output
dtMerged
Col1 A
Col2 B
Col3 C
Col4 D
What I have tried:
I have run a simple foreach
loop on the columns and the rows
to form a bigger datatable.
Also,
DataTable bigTable = new DataTable();
Enumerable.Range(0, ds.Tables.Count).ToList().ForEach(i => bigTable.Merge(ds.Tables[i]));
DataTable bigTable2 = bigTable.Clone();
DataRow[] rowCollection = bigTable.Rows.Cast<DataRow>().ToArray();
DataRow newDR = bigTable2.NewRow();
Enumerable.Range(0, bigTable.Columns.Count).ToList().ForEach(j =>
{
newDR[bigTable.Columns[j].ColumnName] = rowCollection.Select(dr => dr[j].ToString()).Last(s => !string.IsNullOrWhiteSpace(s));
});
注意:我有理由不执行复杂的单个查询并获取单个数据表。
Note: I have reasons not to execute a complex single query and fetch a single datatable.
推荐答案
嗯,一句话:合并英文可能意味着:连接,链接,加入,合并,混合,团结...
如果我理解你,你想合并数据,什么在编程语言中众所周知为加入 [< a href =https://docs.microsoft.com/en-us/dotnet/articles/csharp/language-reference/keywords/join-clause\"target =_ blanktitle =New Window> ^ ]
Well, a word: "merge" in English may mean: connect, link, join, merge, mix, unite...
If i understand you well, you want to merge data, what is well known in programming language as join[^]
var mergedData = from a in tableA.AsEnumerable()
join b in tableB.AsEnumerable() on a.Field<int>("Key") equals b.Field<int>("ForeignKey")
select new
{
A = a.Field<int>("Key"),
B = a.Field<string>("FieldB"),
C = b.Field<int>("ForeignKey"),
D = b.Field<string>("FieldD")
};
DataTable bigTable = new DataTable();
bigTable.Columns.AddRange(new DataColumn[]
{
new DataColumn("Key", typeof(int)),
new DataColumn("FieldB", typeof(string)),
new DataColumn("ForeignKey", typeof(int))
new DataColumn("FieldD", typeof(string))
});
bigTable = mergedData.CopyToDataTable();
欲了解更多详情,请参阅:
加入运算符(LINQ to DataSet) [ ^ ]
LINQ to DataSet中的查询 [ ^ ]
从查询创建DataTable(LINQ to DataSet) [ ^ ]
如果你想合并具有相同结构的表,试试这个:
For further details, please see:
Join Operators (LINQ to DataSet)[^]
Queries in LINQ to DataSet[^]
Creating a DataTable From a Query (LINQ to DataSet)[^]
In case you want to merge tables, which have the same structure, try this:
//create "merged table" with destination structure
DataTable bigTable = new DataTable();
//all tables have to have the same structure!
bigTable.Columns.AddRange(new DataColumn[]
{
new DataColumn("EmpId", typeof(int)),
new DataColumn("EmployeeName", typeof(string)),
new DataColumn("DepartmentId", typeof(int))
});
//copy data into destination table ("merged table")
foreach(DataTable t in ds.Tables)
{
foreach(DataRow r in t.Rows)
{
bigTable.ImportRow(r);
}
}
祝你好运!
Good luck!
这篇关于将数据表数组中的数据列与数据合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!