Keep DataTable Columns Where ColumnName Contains Specific Keyword


As the title says, is there any way to retain only the columns that contain a specific keyword in their column name, without iterating through? I have thousands of columns, so that’s why I’d like to avoid looping.

I can’t seem to do this with LINQ, I tried something like this…

(From x In dt_Test.Columns.Cast(Of DataColumn)
Where x.ColumnName.ToLower.Contains("my specific keyword")
Select x).CopyToDataTable

This seems to return a collection of datacolumns which the “CopyToDataTable” is not a method of, so here’s where I’m stuck. Any help would be greatly appreciated. :slight_smile:



Welcome back to our UiPath community.

  1. First find out all the columns whose header contains specific word as below.

arrColNames = (From c in dtInput.Columns.Cast(Of DataColumn) Where c.ColumnName.Contains("specific word") Select c.ColumnName).toArray

Here, arrColNames is of type Array of String

  1. And then try below expression to fetch only required rows with specific header matching columns.

dtOutput = dtInput.DefaultView.ToTable(True,arrColNames)

Note: If you want duplicate records also then write False instead of True.



Thank you very much!
Your approach works flawlessly.

1 Like

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