Finding out if a value exists in a database column

datatable
studio
datascraping

#1

Hello,

my job is to scrap data from a webpage (it’s only 1 column), then check if a certain value (known earlier) exists in that table/column. Then, if this value exists, I have to perform one action and if it doesn’t - another one.

Do you know how to do it? I tried with ExtractDataTable (output of data scrapping) then for each row, in Body - If Rows(“Column1”).ToString = DesiredValue. The problem is that it looks like it analyzes not if a value is in a table, but if rows are equal. It affects further actions, because they are carried out as many times as many rows are in a column.

Your help will be very appreciated! :wink:


#2

Hello,

Try this:
yourDT.AsEnumerable().Any(Function(x) x("Column1").ToString = DesiredValue)
It will return True if any of the rows match the predicate.


#3

Thank you for your response!

Where do I put it? In Assign or as If condition?


#4

In an If condition. It will check all rows at once, which is what you wanted if I understood correctly.


#5

Well, so far so good - looks like it works.

@andrzej.kniola - you’re the champ! :smiley:


#6

@andrzej.kniola How would I return the row number of DesiredValue?


#7

yourDataTable.Rows.IndexOf(foundRow)
foundRow can be captured by .Select(filter) with .First call or .Single call (depending on if having multiple hits is an exception or not).


#8

@andrzej.kniola if the dt1 itemno matches dt2 itemno, how do I output the value of storeno which is in a same row to matched itemno in dt2.

thank you :slight_smile:


#9

Assuming row from dt1 is already found and stored in r1 variable, itemno and storeno are integers (adjust as necessary):
int foundStoreNo = dt2.AsEnumerable().Single(Function(r2 As DataRow) r2.Field(Of Integer)("itemno") = r1.Field(Of Integer)("itemno")).Field(Of Integer)("storeno")

In a possibly more readable format:

int foundStoreNo = dt2.AsEnumerable() _ 'now we have IEnumerable(Of DataRow)
	.Single(Function(r2 As DataRow) _ 'starting inline predicate for Single call
	r2.Field(Of Integer)("itemno") = r1.Field(Of Integer)("itemno")) _ 'now we have a DataRow returned from .Single
	.Field(Of Integer)("storeno") 'take the field value we are interested in

SIdenote:
Your question is not precise - you are asking to return a row value by matching data tables. What I wrote above requires a row from dt1 you want to match to already be known.
If you really want to match (join) data tables, search for linq join operations (on the forum there should be at least a couple examples).


#10

okay if I am looping through the d1 rows and checking if it matches the d2 row , how do I get the value of another column in d2 which corresponds to the matched value of d1.
Here is an example
d1 = column 1 (1234),(3456) etc …
d2 = column 1 (1234),(6432)etc
column 2 (ABC),(BCD) etc
value of d1 matches D2 in column 1 , but we want to output the value (ABC) which is in the same row as (1234) but different column .

Thank you


#11

Hi Guys,
i have attached one html table , in that table i need to check first Confirmation statement and Annual return description names based on that i need to download pdf from view pdf link …plese help me iam new to ui path