Compare datarow with array

Hi experts,

What should be the correct way to code if I want to compare a column from a dt with an array of string. It didn’t actually loop the whole dt to do the comparison, I have more than 1 row that are matching.

@sophiey

Dt.asenumnerable.where(function(a) array.contains(a(“columnname”).tostring.trim)).CopyToDataTable

Or you can use in for each row in datatable

currentrow.itemarray.any(function(a) ToRemoveArr.contains(a.tostring))

@sophiey

Initialize stringArray (Assign activity)

stringArray={“value1”, “value2”, “value3”}

For Each Row (loop through dt)

If Cond: stringArray.Contains(row(“ColumnName”).ToString)

  Perform actions on matching rows

HI,

Can you share what you want to achieve at last?

If you want to check if the datarow has some items of ToRemoveArr, the following works . (However, we cannot get which column has it)

CurrentRow.ItemArray.Any(Function(o) ToRemoveArr.Contains(o.ToString))

Regards,

Hi Yoichi,

datatable only contain 1 column to compare with the array. some actions to be taken if values are matched

@sophiey

What you did is correct…instead of two loop…if you want to know without the second loop…

Then you can find if it is matched or not by using

ToRemoveArr.Any(function(x) currentRow(0).ToString.Trim.ToLower.Equals(x.Trim.ToLower))

Use the abov ein if condition inside the datatable loop alone…and if atleast one value from array is equal to given row then it would gice true elae it would gice false

When matched if you need the matched values then use the below on the matched or then side

matchedarray = ToRemoveArr.Where(function(x) currentRow(0).ToString.Trim.ToLower.Equals(x.Trim.ToLower)).ToArray

If only one value is matched and you need only then then instead of .ToArray use (0) to get the string value which is matched

Hope this helps

Cheers

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