Copy DataTable Where Column Name Is Not In Array

Hello, is there any way to copy DataTableA to DataTableB except the columns defined in a String Array?

Example String Array:
doNotShowArray = {“Column1”, “Column2”, “Column3”}

Maybe, something like this (no, of course not working):
DataTableB = DataTableA.Select().CopyToDataTable().DefaultView.ToTable(False, Not in doNotShow)

Any way to do this?

@whyyouandi,

  1. Lets say your array as doNotShowArray
  2. Assign : String arr = (From col in DataTableA.Columns.Cast(of System.Data.DataColumn) Select col.ColumnName).ToArray().Except(doNotShowArray).ToArray()
  3. Assign :DataTableB = DataTableA.DefaultView.ToTable(False, arr)

Regards,
Dominic :slight_smile:

2 Likes

Thank you so much!
Not understanding VB.net, I found this hard to do, but it went well.
Learned how to Select with Except, thank you!

In case other people curious too, attach the solution provided by @599712 in .xaml (except that it uses not DataTableA and DataTableB but only B and so on, but the result is the same).

DO NOT SHOW COLUMN.xaml (6.7 KB)