Best way to copy Data from one DataTable to another DataTable with diffrent structure

后端 未结 2 1503
隐瞒了意图╮
隐瞒了意图╮ 2021-01-26 00:02

I am copying data from DataTable to another DataTable with a structure. I have to hardcode columns number in the loop and copy the data in object array.

What will be the

2条回答
  •  春和景丽
    2021-01-26 00:19

    I'm not 100% sure why you want to copy the data to an array before putting it into the VendorInvoiceTable, but either way:

    IEnumerable query = from vendInv in VendorInvoiceStagingTable.AsEnumerable()
                                 where vendInv.Field(VendInvoice.Number) == InvoiceHeader
                                 select vendInv;
    
    // Would this be ok?
    VendorInvoiceTable.Rows.Add(query.First().ItemArray);
    
    // ...or if not, how about this?
    object[] sourceData = query.First().ItemArray;
    object[] targetData = new object[sourceData.Length];
    sourceData.CopyTo(targetData, 0);
    VendorInvoiceTable.Rows.Add(targetData);
    

提交回复
热议问题