How to convert a one-dimensional String array into a System.Data.DataTable

Hello.
I am a user of UiPath Studio 2022.4.6.
I created the following workflow to filter by multiple conditions listed in an Excel sheet.
At step ⑤, I get the following error for variable B. Could you please advise me on how to resolve it?
Error: “Cannot convert a value of type String to System.Data.DataTable”
① Assign the multiple conditions listed in the Excel sheet to variable A.
② variable B = variable A.Split(","c)
③ Read data from a sheet in the target Excel file
④ Copy the original data and create a new table
⑤ Repeat for each element in variable B using ForEach
(set the iteration variable to condition)
⑥ Select only the rows from the original table where the value in column “A” matches condition, and put the results in filterResultTable
⑦ Assign the contents of filterResultTable to ResultTable

Are you trying to filter a data table?
If so, LINQ makes it easy (it only takes one Assign activity).

For example, assuming the column name is (“reading”), the code below extracts from dt1 to dt2 the rows where (“reading”).ToString = “おおさかし”. You can use AndAlso or OrElse to combine multiple conditions.

dt2=dt1.AsEnumerable.Where(Function(row) row("reading").ToString = "おおさかし").CopyToDataTable()

Hello

Could you be using the ForEachRow activity instead of the ForEach activity? Try using the ForEach activity and setting TypeArgument to String.

Thank you for your reply.
What I want to do is extract a data table.
To explain in more detail, I want to filter the extracted Excel data by the specified department codes and delete the matching rows. After deleting them, I want to leave the filter applied.
The [department codes] to be deleted are listed in comma-separated format in Information.xlsx.
The reason for preparing Information.xlsx is that the department codes to be deleted may change, so I want the robot to continue working correctly when the codes are changed in Excel.
That’s it.

Since deleting is the opposite of filtering, you can delete it by negating the condition with Not.
For example, the code below extracts rows from dt1 to dt2, excluding rows where (“所属コード”)=“12345”.

dt2=dt1.AsEnumerable.Where(Function(row) Not row("所属コード").ToString = "12345").CopyToDataTable()

If this answer was helpful, please check Solution. :smiley:

This part is unclear. Are multiple affiliation codes entered in a single cell, separated by commas? Or are the affiliation codes listed vertically in a single column?

A single cell contains multiple affiliation codes, separated by commas.

I think it’s best to avoid handling data in such a complicated way if you want to make future maintenance easier, but if you need to do it as things stand, first get the cell value into str_var.

arr_str = System.Text.RegularExpressions.Regex.Split(str_var, ",")
arr_str = arr_str.Where(Function(x) Not String.IsNullOrWhiteSpace(x)).ToArray

Run the two lines above in an Assign activity to create arr_str, then

You have no choice but to loop through arr_str using a For Each activity.
As Yoichi advised, check that you haven’t mistakenly used the For Each Row activity and that TypeArgument is set to String.

Thank you.
As you pointed out, I was using ForEachRow.

For easier maintenance going forward, we’d like the system to work so that all we need to do is add or remove the department codes listed in information.xlsx.
At the moment, the Excel file isn’t being processed the way we’d expect…

Would the error mentioned in the subject be resolved by doing the following?

I understand the issue.
Did setting the argument type (TypeArgument) of the ForEach activity to String fix this issue?
If this answer was helpful, please check Solution. :grinning_face:

The error has been resolved!
Thank you.

Setting the argument type (TypeArgument) of the ForEach activity to String resolved the error.
Thank you.