从一个数据表中的数据复制到另一个数据表均拥有不同的结构,最好的办法 [英] best way to copy Data from one DataTable to another DataTable with diffrent structure

查看:181
本文介绍了从一个数据表中的数据复制到另一个数据表均拥有不同的结构,最好的办法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我复制从数据表中的数据到另一个数据表,其结构。 我要在循环硬code coulums数量和数据复制的对象数组。 什么将是获得了这幅它的最好方法是什么?

 的IEnumerable< D​​ataRow的>查询=从vendInv在VendorInvoiceStagingTable.AsEnumerable()
                                         其中,vendInv.Field<字符串>(VendInvoice.Number)== InvoiceHeader
                                         选择vendInv;

            [对象] OBJ =新的对象[10]。
            VAR项目= query.First();

            为(中间体IDX = 0; IDX小于10; IDX ++)
            {

                OBJ [IDX] =项目[IDX]

            }

           VendorInvoiceTable.Rows.Add(OBJ);
 

解决方案

我不是100%肯定,为什么你想要把球送入VendorInvoiceTable之前将数据复制到一个数组,但无论哪种方式:

 的IEnumerable< D​​ataRow的>查询=从vendInv在VendorInvoiceStagingTable.AsEnumerable()
                             其中,vendInv.Field<字符串>(VendInvoice.Number)== InvoiceHeader
                             选择vendInv;

//这将是好吗?
VendorInvoiceTable.Rows.Add(query.First()ItemArray。);

// ...如果还是不行,这个怎么样?
[对象] sourceData = query.First()ItemArray。
[对象] targetData =新的对象[sourceData.Length]
sourceData.CopyTo(targetData,0);
VendorInvoiceTable.Rows.Add(targetData);
 

I am copying data from DataTable to another DataTable with a structure. I have to hardcode coulums number in the loop and copy the data in object array. What will be the best way to acheive it ?

         IEnumerable<DataRow> query = from vendInv in VendorInvoiceStagingTable.AsEnumerable()
                                         where vendInv.Field<string>(VendInvoice.Number) == InvoiceHeader
                                         select vendInv;

            Object[] obj = new Object[10];
            var item = query.First();

            for (int idx = 0; idx < 10; idx++)
            {

                obj[idx] = item[idx];

            }

           VendorInvoiceTable.Rows.Add(obj);

解决方案

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<DataRow> query = from vendInv in VendorInvoiceStagingTable.AsEnumerable()
                             where vendInv.Field<string>(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);

这篇关于从一个数据表中的数据复制到另一个数据表均拥有不同的结构,最好的办法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆