When I want to set multiple values in OrderBy of DataTable, how should I write it?

Currently, I am writing the following

dt.AsEnumerable.OrderBy(Function(r) If(Microsoft.VisualBasic.Information.IsNumeric(r(“columnName1”)), CDbl(r(“columnName1”)), 0))).CopyToDataTable()

This time, we need to sort not only the value of “ColumnName1”, but also the value of the column named “ColumnName2”. The image is as follows.

OrderBy ColumnName1 ASC, ColumnName2 ASC

At first, I used DataView, but the values of ColumnName1 and ColumnName2 are numbers, String type. so they are sorted by the first digit as 1, 10, 100.
Therefore, I need to add ColumnName2 as a number to the syntax written above, and add it as the ASC of the second name preference, but I don’t know how to do it.

Can you tell me how to do this?

Thank you for reading.

@Kirigirisu_Coin

From x In datatablevariable.AsEnumerable() Order By convert.ToDouble(x(“Column1)”),convert.ToDouble(x(“Column2”)) Select x).CopyToDataTable

Can you try above expression and let us know

Hope this may help you

Thanks

1 Like

Query syntax and the let statement can help:

(From d in dt.AsEnumerable
Let k1 = If(Information.IsNumeric(r("columnName1")), CDbl(r("columnName1")), 0)
Let k2 = If(Information.IsNumeric(r("columnName2")), CDbl(r("columnName2")), 0)
Order By k1, k2
Select r=d).CopyToDataTable()
2 Likes

As a result of practicing the method you introduced.
I was able to turn them in the required order without any problems.
Thank you very much.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.