I have a datatable
public void CreatTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("First Name");
dt.Columns.Add("Middle Name");
dt.Columns.Add("Last Name");
dt.Rows.Add("A", "B", "C");
dt.Rows.Add("A1", "B1", "C1");
dt.Rows.Add("A2", "B2", "C2");
dt.Rows.Add("A3", "B3", "C3");
dt.Rows.Add("A4", "B4", "C3");
}
I want to arrangement this table sorted by Last Name > First Name > Middle Name
using LINQ
and save to another table (dt2) . Looking for help, thanks
You could use OrderBy
and ThenBy
extension methods.
var newTable = dt.AsEnumerable()
.OrderBy(x=>x.Field<string>("Last Name"))
.ThenBy(x=>x.Field<string>("First Name"))
.ThenBy(x=>x.Field<string>("Middle Name"))
.CopyToDataTable();
Check this Demo
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.