Filter Data Table using multiple variables in LINQ

Hello Everyone.

I have a data table and 2 string variables and need to filter the data table using these two variables for different columns column1 and column2 but sometimes there will be null value in either variables.
Eg:
Variable A = abc, and variable B =“” | Variable A = abc, and variable B =xyz

Column1 Column2
abc xyz
efg uvw
hij yut
klm thj

is there is any query to filter the data table.

Hi,

Can you share expected output?

Regards,

Helo,
i have three scenarios varA filters column1 and varB filter column2
if varA= abc and varB=xyz
|Column1|Column2|
|abc|xyz|

if varA=efg and varB=“” [null]
|Column1|Column2|
|efg|xyz|
|efg|uvw|
|efg|xyz|
regarless of column2 values
vizversa for varA =“”[null]

Hi,

Can you try the following sample?

varA2 =if(String.IsNullOrEmpty(varA),".*","^"+System.Text.RegularExpressions.Regex.Escape(varA)+"$")
varB2 = if(String.IsNullOrEmpty(varB),".*","^"+System.Text.RegularExpressions.Regex.Escape(varB)+"$")

Then

arrDR = dt.AsEnumerable.Where(Function(r) System.Text.RegularExpressions.Regex.IsMatch(r(0).ToString,varA2) AndAlso System.Text.RegularExpressions.Regex.IsMatch(r(1).ToString,varB2)).ToArray

Sample20221224-3aL.zip (3.2 KB)

Regards,

Thanks for the quick response and solution.

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