Pass array variable in Datatable.Select()

Hello,

I am trying to do the following:

  1. Create a variable and assign an array of strings. say variable name = item
  2. dataTable.Select(“Column =” + item) - This is not working as expected.

Requirement: I need to select the columns which are equal to each array item.
say if array = {“hi”, “hello”, “bye”}, I want to filter the datatable for “hi” and then for “hello” and later for “bye”. This iterations are done using For each but I am unable to pass this array variable in Select() method.

How can I do this?

Kindly help!

Many thanks in advance!

Hello,

I would just use the Array variable for this using either LINQ or Lambda vb.net
For example:
arrVariable.Where(Function(item) item = “hi” or item = “hello”).ToArray()
will do multiple criteria

(From item In arrVariable Where item = “bye” Select item).ToArray()
will do just one and you can have 3 of these for each word

Thanks.

Hi ClaytonM,

Thanks for your response.

Can this be used to filter Data table or excel sheet?

@RPA_sav
Hi,

Yeah you can use this for DataTable like:
datatableVariable.AsEnumerable().Where(Function(row) row(“column”).ToString = “text”).CopyToDataTable()
or
(From row In datatableVariable.AsEnumerable() Where row(“column”).ToString = “text” Select row).CopyToDataTable()

For Excel Sheet filtering you would need to use keystroke combinations or element clicks, however you can store the sheet into a datatable, filter it the way you want, then output it back to the Excel file, which is probably what I would do (unless you are wanting to use the built-in Filter feature of Excel).

Thanks.

1 Like

Hi @ClaytonM,

can you please give an example of where I put the code?
Thanks

Martin

Hi @martin.grimaldo

There are different ways to utilize the filtered datatable, but ideally you can just use an Assign activity to store your new datatable or array of datarows into a variable, then process them using a ForEach activity.

Similarly, you can do the same to create an array of rows (from within the original datatable) to process, which in many cases is a better use since you can update the original datatable with values. For example, Assign arrRows(i).Item(“column”) = “newtext” and it updates it in the datatable

1 Like

hi,

Could you help me? I need to save into an array of rows the result from a filtered datatable using Select. But it always returns me zero data, I have already verified the data in datatable with an output message and everything seemed fine.

Thanks

Hi may I drop some similar question here?

I’m trying to implement something like this
DT.Select(“ColumnName in {“item1”, “item2”, “item3”}”).copytodatatable

How can I replace this list/array {“item1”, “item2”, “item3”} with a list/array-type variable?
Basically what I need to do is to convert Datatable.Columns into {“”, “”} clause.

I have tried
For each row in DataTable
Add to collections {collection: new List of String, Item: row.item(“columnName”)}.ToString

Assign
New DT = DataTable.Select((“ColumnName in List”)

then I got this error:
“The items following the in keyword must be separated by commas and be enclosed in parentheses uipath”

I have a solution of filtering a data table by an array of strings. Here are the steps:

  1. take the array in a variable(arrayInput)
    2)take the data table in a variable (dtInput)
  2. take for each and loop through array(arrayInput).
  3. take an assign activity inside for each
    Assign newDtTemp (new datatable variable) = dtInput.select(“[column name]+'” item.tostring+'").copytodatatable()
    4)take invoke method and take target object as dtOutput(a new datatable) and method is merge. In the properties add in collection as datatable argument pass DtTemp.
  4. outside the loop you will have your data in the DtOutput variable with all the required filters.

This worked for me please check and revert back if it works for you as well.

Thank you,
Suman

It doesnt work.
if it is working for you,can you attach your code.

Hi,
Did you find a solution ? I am facing the same issue :confused: