Remove data columns

Hi All,

I am having a datatable with below data columns.

Name, Age, Gender…Column1, Column2…ColumnN.

I need to remove all columns having string “Column” in it. Please advise how it can be achieved.

Thanks!

@Kapil , Let’s Say you have the Datatable variable as DT. We would require to Collect the Column names and Perform a Filter on this Column Names list to keep only the names which doesn’t contain Column in it. Now this filtered list can be used in DefaultView.ToTable() method to keep only the required Columns.

Get Column names and Filter it, we can use an Assign Activity and do the below :

filteredColumnNames = DT.Columns.Cast(Of DataColumn).Where(Function(x)Not(x.ColumnName.ToString.ToLower.Contains("column"))).Select(Function(x)x.ColumnName.ToString).ToArray

Here, filteredColumnNames is of the type Array of String.

Next, we Keep only the Required Columns in the Below way using an Assign Activity :

DT = DT.DefaultView.ToTable(false,filteredColumnNames)

This should keep only the column names that you require.

Hello @Kapil

If you know the index of columns to be removed you can directlt use Remove Data Columns activity.

There you can provide the range and it will remove all those columns.

Hi @supermanPunch , filteredColumnNames seems to be of type array of datacolumns and not array of string. Can you please confirm.

Also if it is array of datacolumns, then how can that be converted into data table?

Thanks!

@Kapil ,

Apologies.

I have updated the Post above, Could you Check the Query and Let us know if it worked.

(From c in DT.Columns.Cast(Of DataColumn)
Select cn = c .ColumnName
Where Not cn.ToLower.StartsWith("column")).toArray
1 Like

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