4
votes

I have a scenario where-in I have a DataTable with certain columns "Col1, Col2, Col3". I want to copy just "Col2, Col3" into another DataTable which has a primary key "ID". What is the best way to copy them. There are 5000+ records and performance is a key factor.

I tried with Select, DefaultView.RowsFilter but no success. I know one option is to loop through all records an copy data one by one in second DataTable. But wanted to know a better way.

1

1 Answers

4
votes

try this code (visual basic):

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 want distinct rows then put true in first ToTable argument.