Filter datatable using Array/List of string with multiple condition

Hi
I m trying to find a more effective way to do the filtering.
dt.AsEnumerable.Where(function(r)stringArry.Contains(r(“ColumnName”).ToString())).CopyToDataTable()
but i want to add 4 more condtions on it. for example
dt.AsEnumerable.Where(function(r)stringArry.Contains(r(“ColumnName”).ToString() AND (x(“Column2”).Equals(“”) and (x(“column3”).equals(“”) AND anotherStringofarray)

how do i achieve with one line of code? I tried. select as well but didn’t really work
Any advice.
Thanks

@whitestar - pls try below

DT.AsEnumerable().Where(Function(row) stringArry.Contains(row(“ColumnNam0”).ToString()) AND row(“ColumnNam1”).ToString().Equals(“ABC”) AND row(“ColumnNam2”).ToString().Equals(“XYZ”) ).CopyToDataTable()

it will work - however contains is the slowest section in the linq part…

5 Likes

Do you have any suggestion to achieve it with most effective way ? and faster way ?
I don’t want to use filter table activity as we may get more string to add in the array/list.

Thanks

1 Like

Hi,

If you want to filter datatable by dynamic multiple condition with single line, the following might help you, for example.
You can just add dictionary item if you want to add condition.

dt.AsEnumerable.Where(function(r) dict.All(function(x) x.Value.Contains(r(x.Key).ToString))).CopyToDataTable()

Note: This way is high maintainability but might not be very fast.

Regards,

2 Likes

@whitestar

ColList: the ColIndex (ColNames would also be possible)
FilterList: the Values on which should compaired

(From d In dtData.AsEnumerable
Where ColList.Select(Function (x) FilterList.Contains(d(x).toString.Trim)).All(Function (b) b)
Select d).toList

EDITED and simplified:

(From d In dtData.AsEnumerable
Where ColList.All(Function (x) FilterList.Contains(d(x).toString.Trim))
Select d).toList

It is looking for each col as defined by ColList if its Value is contained in SearchList.
If this true for All then the is getting past to the result

Find Demo here:
DT_MultiColPlusList_Filter.xaml (8.4 KB)

2 Likes

just updating. all of the provided solution are works. i ended up using GBK due to simplified . it’s not the fastest but that will do for now. :slight_smile:

I was going to close it but i want to know if i can use Equal rather than contains for each of the array list ? I tried changing to .Equals. but not working as expected

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