Select to sort in ascending order a datatable

Hi all,

I am trying to ascending sort a column (general type in excel) from a datatable, with the following expression:

(From x In dt.AsEnumerable() Order By convert.ToDouble(x(“amount”)) Select x).CopyToDataTable

Input string is not in the correct form. Any help?

Thanks in advance


Please try like this

dt.AsEnumerable.OrderBy(function(x) If(IsNumeric(x("amount")),CDBL(x("Amount").ToString.Trim),0)).CopyToDataTable

If the given string is blank or not in numeric format we might get that error…so first I am checking if it is numeric else assigning 0


1 Like

Shown this error:

cannot be inferred from these arguments. Specifying the data type explictly might correct this error


Can you show what you tried…As I did the same and can get the output as expected

Initial data

Used assign with above formula


Or Alternately try moving IsNumeric to last

dt.AsEnumerable.OrderBy(function(x) If(x("amount").ToString.Trim.IsNumeric,CDBL(x("Amount").ToString.Trim),0)).CopyToDataTable


1 Like

Hi @Kumar802 ,

Could you check with the below Expression :

DT.AsEnumerable.OrderBy(Function(x) If(x("Amount").ToString.IsNumeric,CDbl(x("Amount").ToString.Trim),0)).CopyToDataTable

This is the input excel file

This is the output with the expression:


I can’t figure out what’s wrong.


I guess it is because of decimal places being separated with comma

please try like this

DT.AsEnumerable.OrderBy(Function(x) If(x("Amount").ToString.Replace(",",".").IsNumeric,CDbl(x("Amount").ToString.Trim.Replace(",",".")),0)).CopyToDataTable



As the culture format of number is different instead of replace you can as well use tryparse to parse with culture of comma as decimals

dt.AsEnumerable.OrderBy(function(x) If(Double.TryParse(x("amount").ToString,NumberStyles.Number,CultureInfo.GetCultureInfo("it-IT"),0),Double.Parse(x("amount").ToString,NumberStyles.Number,CultureInfo.GetCultureInfo("it-IT")),0)).CopyToDataTable

Please let me know if you face issues