Simple Solution to Searching for Specific Value in DataTable

datatable

#1

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.


#2

may you give us an example of table ?

Your request is a “value exists” result?


#3

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


#4

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


#5

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


#6

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.


#7

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?


#8

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.


#9

Great. Thank you!!


#10

@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.


#11

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: