Filter Data Table based on two columns

Hello,I’m having trouble in filter datatable based on two conditions in two columns.
For example, my data table looks like:

Location Type
AU AA
JP BB
CN EE
HK CC
CN AA
CN CC
JP DD
HK DD
CN DD

And the condition is: Remove the rows where

  1. Location = “CN” AND
  2. Type = “AA” or “CC”
    Note that the row need to be removed if and only if the two conditions are met.

So the output should be like:
Location Type
AU AA
JP BB
CN EE
HK CC
JP DD
HK DD
CN DD

I’ve tried this query:
(From r In myDT.Select()
Where (r(“Location”).ToString <> “CN” And
(r(“Type”).ToString <> “AA” Or r(“Type”).ToString <> “CC”))
Select r).CopyToDataTable

but it always turns out that all the “CN”'s are removed.

I also tried to use ‘for each row’ and if statement to find rows that met the two conditions, but I stuck in remove those rows from Data Table.

Could anyone help me with this? Thank you!

Hi,

Can you try the following expression?

(From r In myDT.Select()
Where (Not (r("Location").ToString = "CN" AndAlso
(r("Type").ToString = "AA" OrElse r("Type").ToString = "CC")))
Select r).CopyToDataTable

Regards,

1 Like

This works! Thanks a lot!

1 Like

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