16

Q: If I have two DataTables like this :

Dt1(emp_num,emp_name,type)

Dt2(emp_num,emp_name,type)

I wanna to Union them and order the result by emp_name.

Raidri
  • 17,258
  • 9
  • 62
  • 65
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392

2 Answers2

18
var dt1 = new DataTable(); // Replace with Dt1
var dt2 = new DataTable(); // Replace with Dt2

var result = dt1.AsEnumerable()
            .Union(dt2.AsEnumerable())
            .OrderBy (d => d.Field<string>("emp_name"));
Razor
  • 17,271
  • 25
  • 91
  • 138
6

i think this code will help u to do it without using entity...

Dt1.Merge(Dt2);
Fethullah Kaya
  • 194
  • 4
  • 16
  • 1
    It works as expected. If you don't set any primary key on base table (in above example, Dt1), then merge works like 'UNION ALL'. It means if there are rows with same column values in base table and table to be merged, then both rows will be present in base table after merge. But if primary key is set on base table, then only those rows which don't exist in the base table as per the primary key, will be added in base table. Hence, no duplicates if primary key is set on base table. – Tushar R. Oct 30 '20 at 08:53