Simple Solution to Searching for Specific Value in DataTable

I have tried searching for a simple VB script that would go through a DataTable (derived from excel) looking for a specific value and returning a boolean, however, I have not been able to find one. I feel as though this should be a simple script but I can’t seem to master it. Does anyone have a solution or suggestions? Thank you.

may you give us an example of table ?

Your request is a “value exists” result?

The table is literally one column that has a header and 7-8 values, that’s it haha.

This code will work: if you change valueExpected

dataTable.Rows.Cast(of DataRow).Any(function(row) row(0) = valueExpected)

dataTable is a DataTable and valueExpected is an object if you want it to be a sstring you must add .tostring after row(0).tostring

1 Like

Thank you very much. One last thing…so how does this return a boolean for “value exists”? Would that be handled with the arguments?

Any is a linq expression that give you true if it exist a value in the list that fullfills the expression

You can also use Where instead, in case you want to get a list of all rows matching the expression.

Ok that makes sense. So could you store the result of this code into a variable in order to use in an “if” statement later on?

write it in the if statement and it will works :slight_smile: in case you want to store it you must declare a boolean variable and use an assign activty but its not required.

Great. Thank you!!

@Ignacio_Insua_Feito Based on your replies, the expression u have given works for datatable with one column. Is it works if we want to search for a value in datatable with multiple coulmns also.

My statement works fine always :slight_smile: but may change if you want to validate more than one condition :slight_smile: i will search and edit to reffer to another comment where i explain further :slight_smile:

edit: