Deleting columns with "Keyword" inside a datatable

Good Morning Everyone,

I am trying to delete specific columns in a datatable. This columns all have one thing in common, they contain “URL” in the name.
How can I achieve this?

Thanks for the help :slight_smile:

can you please clear the use case:

  • you want to remove columns where the column name contains “URL”

OR

  • you want delete column values, where the value contiains “URL”
    • a specific column OR any column

@Duarte_Valente,

Use this LINQ in Invoke Code activity.

Dim columnsToRemove = dt_Input.Columns.Cast(Of DataColumn)().Where(Function(col) col.ColumnName.Contains("URL")).Select(Function(col) col.ColumnName).ToList()
 For Each colName In columnsToRemove
        dt_Input.Columns.Remove(colName)
Next

Pass your datatable to invoke code as InOut

Thanks,
Ashok :slight_smile:

in that case we can do (essential modelling)

For each Activity - item in YourDataTableVar.Clone.Columns | TyeArgument: DataColumn

  • If Activity: Condition: item.ColumnName.ToUpper.Contains(“URL”)
    • Then: remove Data Column Activity

Kindly note:

  • we used for the interation the Clone Method on the origin Datatable. This avoids an exception thrown when the looped collection is changed (what we would do, when removing columns)

The table is represented below!
The goal is to remove the columns Move URL, Type URL, etc…
I would like to do this without using the filter activity since there are several columns with URL in it.

Tabular Data Representation

ID Name Move URL Type Type URL Power Accuracy PP
1 Growl Growl Normal Normal 100% 40
7 Leech Seed Leech Seed Grass Grass 90% 10
13 Vine Whip Vine Whip Grass [Grass](https://bulbapedia.bulbag
1 Like

perfect

so we can do as mentioned:

Another option is:

Assign Activity
arrKeepCols | String Array =

yourDataTableVar.Columns.Cast(Of DataColumn).Select(Function (x) x.ColumnName).Where(Function (x) Not x.ToUpper.Contains("URL")).ToArray

assign Activity =
dtCleansed = yourDataTableVar.DefaultView.ToTable(false, arrKeepCols )

When I try to create the item variable I get this Error:

we cannot derive the modeling motivations but would doubt it

as we mentioned:

do not use / involve arguments or variables which are not defined or are null

Hello Ashokkarale!
I am trying out your suggestion, although I confess that I don’t understand the code that you suggested :slight_smile:

For that reason, how should I handle this Error message?

@Duarte_Valente
we recommend no to work on multiple approaches in parallel. Just select one implement it, and explore the other options / approaches afterwards also.

About the ambigious problem: use the full Qualifier: System.Data.Datcolumn instead of DataColumn

Thank you very much PPR !

All I had to do was to write currentDataColumn.ColumnName.ToUpper().Contains(“URL”) and the bot worked!

Have a nice day :slight_smile:

Perfect, so lets close topic by marking the solving post as solution:
Forum FAQ - How to mark a post as a solution - News / Tutorials - UiPath Community Forum

@Duarte_Valente,

You didn’t configured properly
Here is working solution.

Excel file.
Test.xlsx (8.6 KB)

Before:
image

After:
image

Here is workflow:
URL Columns.xaml (7.7 KB)

Thanks,
Ashok :slight_smile:

I have one question,

In your xaml file, the variable is named dt, but in the Invoke code is dt_input.(…)
Why is that?

Indeed, I didn’t have the out_table variable created! I fix it, but the code is still not working.

Thank you PPR! I will as soon as I manage to figure out Ash suggestion :pray:

@Duarte_Valente,

Here we are mapping it.

left side is Code variable and right one is Workflow variable.

Thanks,
Ashok :slight_smile:

Thank you so much for you time Ashokkarale!

I am just starting and there is so much I don’t know.
I configured those arguments, but I still get an error. Don’t know what else to do.

Thank you anyways

No problem @Duarte_Valente,

What’s the error?

Thanks,
Ashok :slight_smile:

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