Copy selected columns from one DataTable to another

I have a where-in script I have a DataTable with specific columns "Col1, Col2, Col3". I want to copy only "Col2, Col3" to another DataTable that has the primary key "ID". What is the best way to copy them. 5,000+ records and performance is key.

I tried with Select, DefaultView.RowsFilter, but did not succeed. I know that one option is to loop through all the records of the copy data one by one in the second DataTable. But I wanted to know the best way.

+4
source share
1 answer

try this code (visual base):

dim dt1 as new datatable() dim dt2 as new datatable() dim dv as dataview = dt1.defaultview dv.ToTable(false, new string[] { "col1", "col2" }) 

If you need different lines, then put true in the first ToTable argument.

+3
source

Source: https://habr.com/ru/post/1341749/


All Articles