public static DataTable DatableInnerJoin(DataTable FirstTB, DataTable SecondTB, DataColumn[] FJC, DataColumn[] SJC)
{
if (FirstTB == null || SecondTB == null)
return null;
if (FJC == null || FJC.Length <= )
return null;
if (SJC == null || SJC.Length <= )
return null;
DataTable table = new DataTable("Join");
using (DataSet ds = new DataSet())
{
ds.Tables.AddRange(new DataTable[] { FirstTB.Copy(), SecondTB.Copy() });
DataColumn[] parentcolumns = new DataColumn[FJC.Length];
for (int i = ; i < parentcolumns.Length; i++)
{
parentcolumns[i] = ds.Tables[].Columns[FJC[i].ColumnName];
}
DataColumn[] childcolumns = new DataColumn[SJC.Length];
for (int i = ; i < childcolumns.Length; i++)
{
childcolumns[i] = ds.Tables[].Columns[SJC[i].ColumnName];
}
DataRelation relation = new DataRelation("Relation_Table", parentcolumns, childcolumns, false);
ds.Relations.Add(relation);
for (int i = ; i < FirstTB.Columns.Count; i++)
{
table.Columns.Add(FirstTB.Columns[i].ColumnName, FirstTB.Columns[i].DataType);
} for (int i = ; i < SecondTB.Columns.Count; i++)
{
if (!table.Columns.Contains(SecondTB.Columns[i].ColumnName))
table.Columns.Add(SecondTB.Columns[i].ColumnName, SecondTB.Columns[i].DataType);
else
table.Columns.Add(SecondTB.Columns[i].ColumnName + "_Second", SecondTB.Columns[i].DataType);
}
table.BeginLoadData();
foreach (DataRow firstrow in ds.Tables[].Rows)
{
DataRow[] childrows = firstrow.GetChildRows(relation);
if (childrows != null && childrows.Length > )
{
object[] parentarray = firstrow.ItemArray;
foreach (DataRow secondrow in childrows)
{
object[] secondarray = secondrow.ItemArray;
object[] joinarray = new object[parentarray.Length + secondarray.Length];
Array.Copy(parentarray, , joinarray, , parentarray.Length);
Array.Copy(secondarray, , joinarray, parentarray.Length, secondarray.Length);
table.LoadDataRow(joinarray, true);
}
}
}
table.EndLoadData();
}
return table;
}