Assistance filtering datatable, editing and adding to original datable

Hello,

I am working on a process where a .csv file is read into a datatable(1). In that datatable we then filter out rows that are already marked as complete and then save the remaining uncompleted rows as an array of datarows(2). We do this to save time unnecessarily iterating through rows we’ve already completed.

Next we use a ‘For Each’ activity to cycle through the new list of rows to update them and mark as complete.

With this done, we’d like to add the newly added information from the updated datarows (2) back to the original datatable (1)

Does anyone know how this might be done?
Currently, I can only seem to completely overwrite the original (1) with the new results(2) Instead we want to preserve the rows we filtered out and only add back in the updated rows.

(1) dt_original
(2) dr_filteredRows

@DaneMcDaniel

Is there any unique value column ?

If so we can use that unique column value to identify the row and update the value

A sample linq query is here which can be used in invoke code

Send maindt and filtereddt

Maindt.AsEnumerable.Where(function(x) Not String.IsNullOrEmpty(x("Status").ToString)).ToList.ForEach(sub(r) r("Status") = If(filtereddt.AsEnumerable.Where(function(x) x("UniqueCol").ToString.Equals(r("UniqueCol").ToString)).Count>0, filtereddt.AsEnumerable.Where(function(x) x("UniqueCol").ToString.Equals(r("UniqueCol").ToString))(0)("Status").ToString,""))

If you dont want linq then loop on filtered dt and use lookup on maindt and can update the values

Cheers

Here’s a clear approach to solving your issue in UiPath:

Filter the DataTable:

Use the DataTable.Select method to filter out rows marked as complete:

dr_filteredRows = dt_original.Select(“Status <> ‘Complete’”)

This will give you an array of DataRow objects that are not marked as complete.
Process the Filtered Rows:

Use a For Each loop to iterate through dr_filteredRows and update the necessary information. Ensure that you update the data directly in the DataRow objects.
Update Original DataTable:

Instead of overwriting the original DataTable (dt_original), you can update the rows directly by
referencing their index.
Here’s how:
In your loop, identify the index of each DataRow from the filtered array and update the corresponding row in dt_original:

For Each dr In dr_filteredRows
dt_original.Rows(dt_original.Rows.IndexOf(dr))(“ColumnName”) = dr(“UpdatedColumnName”)
Next
Save the Updated Data:

After processing and updating, dt_original will now have both the unmodified rows (those that were previously marked as complete) and the updated rows (from the filtered and processed ones).
By following these steps, you ensure that the original

DataTable is not overwritten, and only the relevant rows are updated.

Thank you two for the fast response!
These are great, I’ll put them to work right away

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