Linq query with regular expression

Hi Team ,

Can anyone tell me what is wrong with the below syntax, I am trying to filter data table

dtMainReport.AsEnumerable.Where(Function(x) (convert.ToInt64(System.Text.RegularExpressions.Regex.Matches(x(“column”).ToString,“[0-9]\d*(.[0-9]\d+)?”)(0))>50 )).CopyToDataTable

please share some sample values representing the input for the regex. Currently we do see some possible issues with the Linq and regex. Thanks

In one column there are values like

200 persons
100 persons
50 persons
10 persons

I have to filter this data table where persons are < 50

we can anchor

So a LINQ could look like this:

(From d in dtMainReport.AsEnumerable
Let cnt = System.Text.RegularExpressions.Regex.Match(d("column").ToString,"\d+(?= persons)").Value.Trim
Where CInt(cnt) < 50
Select r=d).CopyToDataTable

Depending on the expected data quality the statement can be adoptet on some more checks and handling defensive an empty filter result

1 Like

Thankyou for the response , I just want to correct one thing that in place of “Person” there can be any string like

200 as Ram
30 as shyam
150 gopal

give a try with folloing pattern taking the first found number:

I have kept it in assign and it is showing “type integer is not valid”

I am storing it in DT variable

check for any values e.g. blank , no valid string parseable to int
This was mentioned by:

1 Like


Try below expression.

dtMainReport.AsEnumerable.Where(Function(x) Cint(System.Text.RegularExpressions.Regex.Match(x(“ColumnName”).ToString,"\b\d+\b").Value)>50).CopyToDataTable

1 Like

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