Inner join two DataTables using Linq

By Mash B

Inner join two datatables using linq and get Datatable resultset.

Ex.
DataTable
dt1 = new DataTable();
DataTable
dt2 = new DataTable();

// Here assumed both the datable are filled with some data
// Below linq query joins two table based on ID column and gets the resultset

DataTable
result = (from t1 in dt1.AsEnumerable()
                                    join t2 in dt2.AsEnumerable() on t1.Field<int>("id") equals t2.Field<int>("id")
                                   select t1).CopyToDataTable();

Inner join two DataTables using Linq  (4008 Views)