How to filter a column Values in a datatable

Hi team,

I am working on a scenario, where I want to filter the Values of a specific column in a data table. I have used datatable.select(“[Column Name]”=condition but the regex expression in giving errors. The columns contains the Zip values of different regions. In that Zip column some times this type of zip value occurs “MS23SX-7” so I want to remove the entire row in which this type of values occur in Zip column.

Any help in this regard will be highly appreciated!

Thank You
Regards
enthusiastic

give a try on LINQ or let us know more details on value, pattern and we will help you on the LINQ

In general it would look like this

(From d in YourDataTable.AsEnumerable
Where Not System.Text.RegularExpressions.Regex.IsMatch(d(ColNameOrIndex).toString, YourRegexPattern)
Select r = d).CopyToDataTable
2 Likes

Hi,

I have modified the LINQ query you provided according to my conditions the query is like that:
(From d In dt.AsEnumerable
Where Not System.Text.RegularExpressions.Regex.IsMatch(d(Zip).toString, “[a-zA-Z]”)
Select r = d).CopyToDataTable

error is this: ZipColumn_test: ‘name’ argument cannot be null.
Parameter name: name

Kindly have a look into it.

Thanks and Regards

enthusiastic

Hi @enthusiastic,

I have rephrased your expression like below:
FilterDatatable =

(From d In FilterDatatable.AsEnumerable
Where Not System.Text.RegularExpressions.Regex.IsMatch(d("Zip").toString, "[a-zA-Z]")
Select r = d).CopyToDataTable

And was able to see the resultant in the excel without the values which you have mentioned in the query like “ MS23SX-7

Is your issue is solved or do I miss anything

Also, it would be helpful if you provide us with a screenshot of the error which you’re facing.

Regards,
@90s_Developer

1 Like

when we pass the columnname as a string value then we need to surround it with quotes
d(“Zip”)

A d(ZiP) is parsed as using a variable with the name Zip

Just correct it by surrounding it with " and it should work

1 Like

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