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