I have two datatables. One is a superset of another. Both have one column StageID common. I need to merge these two tables together so that the subset datatables data gets stored in the correct row of superset table.
This is what i did to join the data after retrieving the superset data in JoinTable and subset data in DTMaterialProperties table
foreach (DataColumn dcMaterialProp in dtMaterialProperties.Columns)
{
if (dcMaterialProp.ColumnName != "StageId" &&
JoinDataTable.Columns.Contains(dcMaterialProp.ColumnName))
{
JoinDataTable.Rows[rowCount][dcMaterialProp.ColumnName] =
dtMaterialProperties.Rows[0][dcMaterialProp.ColumnName];
}
}
But this was not efficient as it takes a lot of time in looping through. Please Help me in finding a better way to do this.
If you can use LINQ, an easy way would be to use the Join
extension method.
See Join Method-Based Query Syntax Examples (LINQ to DataSet)
If you don't have access to LINQ to DataSet
...you can check out this link from MSDN on how to implement a DataSet JOIN Helper Class.
HOW TO: Implement a DataSet JOIN helper class is Visual C# .NET
What you are really looking for seems to be a relationship. If you add both datatables to the same dataset, you can define the relationship. See Define a relationship between two tables in a DataSet in VB .NET
Then, all you have to do is parentRow.GetChildRows()
to get the associated child rows.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.