None
None

Reputation: 5670

Reorder DataTable rows with comma separated values

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

Answers (1)

mrida
mrida

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

Related Questions