How to union two data tables and order the result

Anyname Donotcare picture Anyname Donotcare · Jan 3, 2012 · Viewed 14k times · Source

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.

Answer

Razor picture Razor · Jan 3, 2012
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"));