"And" is not working in datable.select

The “AND” or “OR” in the Datatable.select is not working.

dtMasterTable.Select("[Column5] = ‘var1’ AND [Column9] = ‘var2’ ").Count.ToString

I am checking, if the particular combination of records from datatable 1(Iterating this Dt) is present in the datatable2 (dtMasterTable).

If I select the conditions separately, the data is retrieved but when I combine the conditions with “AND”, it’s not working.

Please let me know if I am missing something in the statement. Thanks in advance!!

@pk123
does it work if you change AND to OR?

and is there a row where Column5 = var1 and Column9 = var2?

“OR” also is not working.

Yes, there is a matching row but I did some cleanup to get the matching data.
Below is the sample data

dtMasterTable:
Column5 123
Column9 1

dtTable2
column0 123
column4 '1

var1 = row(0).ToString().Trim()
var2 = row(4).ToString().Trim().Substring(1)

Removing ’ from row(4)

are var1 and var2 variables?

if so it should be

dtMasterTable.Select("[Column5] = '"+var1+"' AND [Column9] = '"+var2+"'").Count.ToString

you can also print this line before the select statement to check your condition is correct
"[Column5] = '"+var1+"' AND [Column9] = '"+var2+"'"

This solution worked. Thank you so much!
Appreciate your quick response @jack.chan

Can you pls clarify the below syntax as well?

Not working dtMasterTable.Select("[Column5]=’ “+var1+” ’ AND [Column9]=’ “+var2+” ’ ")
vs
Working dtMasterTable.Select(“[Column5] = '”+var1+“’ AND [Column9] = '”+var2+“'”)

Wondering, if the spacing matters for the quotes.

1 Like

it is the spacing ,
let say
var1=‘abc’
var2=‘def’

dtMasterTable.Select("[Column5]=’ “+var1+” ’ AND [Column9]=’ “+var2+” ’ ")
will translate to

dtMasterTable.Select("[Column5]=' abc ' AND [Column9]=' def '")

which is wrong

1 Like

Thanks for the clarification @jack.chan

1 Like

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