Filtering only numeric values through LINQ

datatable
linq

#1

Hi
If we have a column with numeric, alphabetical and alphanumeric values, how can we use LINQ to filter out only the numeric values?

I tried something like DT.Select(“ColumnName NOT LIKE”+regex) but it isn’t working.


#2

In this case, you might use the .Where in vb.net instead of .Select cause I think it’s more flexible and easier to understand.

Example,
dt.AsEnumerable().Where(Function(row) IsNumeric(row(column).ToString.Trim)).ToArray()
will return an array of DataRows
or
dt.AsEnumerable().Where(Function(row) IsNumeric(row(column).ToString.Trim)).CopyToDataTable()
will convert your filtered table to a dataTable

You can also use a Regex pattern as your condition, but if you just need to know if it’s a number you can use IsNumeric()

Hope this helps. Thanks.


#3

Hi @ClaytonM

Thanks…this works!

Do you happen to know if there’s any online resource to know about such commands? Since I don’t have prior .Net experience I wasn’t aware of commands such as dt.AsEnumerable().Where()


#4

I don’t really know of a good resource, but what I do normally (cause I don’t remember how to do certain things) is search in Google and you’ll find many examples in C# and Vb.net. UiPath uses vb.net mostly but you can adjust the syntax from C# if you know the difference (which is basically vb.net uses “Function(x)” instead of “x =>”).