Reputation: 5670
I have a DataTable dtData
ID name
bs2 bach
js5 hash
lk3 kom
and I have a string IDorder
which contains
js5,bs2,lk3
Now I want to reorder my DataTable rows by the order of ID's in IDorder
Expected OutPut
ID name
js5 hash
bs2 bach
lk3 kom
How can I achieve this in C# ?
Upvotes: 0
Views: 87
Reputation: 1157
string IDorder = "js5,bs2,lk3";
DataTable dtData = new DataTable();
//create columns for datatable ID and name
var ordered = dtData.AsEnumerable().OrderBy(x => IDorder.IndexOf(x["ID"]));
Upvotes: 3