public static DataTable JoinTable(DataTable firstTable, DataTable secondTable, string firstTableKey, string secondTableKey)
{
DataTable dtResults = secondTable.Clone();
foreach (DataColumn dataColumn in firstTable.Columns)
{
if (!dtResults.Columns.Contains(dataColumn.ColumnName))
dtResults.Columns.Add(dataColumn.ColumnName, typeof(string));
}
foreach (DataRow dataRow in firstTable.Rows)
{
DataRow dtRow = dtResults.NewRow();
foreach (DataColumn dataColumn in firstTable.Columns)
{
dtRow[dataColumn.ColumnName] = dataRow[dataColumn.ColumnName].ToString();
}
foreach (DataRow finalRow in secondTable.Select("[" + secondTableKey + "] = '" + dataRow[firstTableKey].ToString() + "'"))
{
foreach (DataColumn dataColumn in secondTable.Columns)
{
dtRow[dataColumn.ColumnName] = finalRow[dataColumn.ColumnName].ToString();
}
break;
}
dtResults.Rows.Add(dtRow);
}
return dtResults;
}
No comments:
Post a Comment