【发布时间】:2015-05-08 20:07:17
【问题描述】:
我在对应的 DataTable 中有一个像 {"Foo", "Bar"} 的 DataRow,它有 2 个字符串列。我想将这一行复制到一个新的 DataTable 中,它有 3 个字符串列,其中第 0 个是我要传入的新值。所以结果行应该看起来像 {"My", "Foo", "Bar"}。
冲洗,重复。另一个复制操作类似于 {"Bob", "Smith"} => {"My", "Bob", "Smith"}。
我尝试在 Array.Copy() 和 myDataRow.ItemArray.CopyTo(otherDataRow.ItemArray) 中使用 DataRow.ItemArray。然后我读到这不起作用,因为 ItemArray 有点只读,尽管智能感知告诉你不然。
当然我可以编写自己的循环来逐列复制值,但是.. 为什么这不是内置在库中的吗?
示例代码:
DataTable dtDestination = new DataTable();
DataTable dtSource = new DataTable();
dtSource.Columns.Add("Str1");
dtSource.Columns.Add("Str2");
dtDestination.Columns.Add("Str0");
dtDestination.Columns.Add("Str1");
dtDestination.Columns.Add("Str2");
dtSource.Rows.Add("Foo", "Bar");
dtSource.Rows.Add("Bob", "Smith");
foreach (DataRow drSrc in dtSource.Rows)
{
DataRow drNew = dtDestination.NewRow();
drNew["Str0"] = "My";
//this doesn't work
Array.Copy(drSrc.ItemArray, 0, drNew.ItemArray, 1, drSrc.ItemArray.Length);
//this doesn't work either
drSrc.ItemArray.CopyTo(drNew.ItemArray, 1);
//I want to add the "new row" to my destination table,
//*WITH* the contents from the source-row plus the "My" value in the 0th column!
dtDestination.Rows.Add(drNew);
}
【问题讨论】:
-
迭代有什么问题?
drNew[1] = drSrc[0]; drNew[2] = drSrc[1] -
这就是我的意思...我可以通过循环轻松做到这一点,很好。我在问为什么这种东西没有内置到框架中,或者至少没有内置到某个可以调用的库中。
-
因为它简单易行。例如,将比数组复制执行得更好。
-
@Zer0 如何表现更好?它仍然是 O(N),其中 N = 数组大小 = 行中的列数。但我确实明白你的意思,实现自己很简单——没有
DataRow.CopyValuesFrom(otherDataRow, startingIndex)或其他东西似乎很愚蠢。 -
因为在您调用
NewRow时已经创建了ItemArray。它增加了内存压力,为 GC 产生了更多垃圾。接受的答案是每行创建 2 个数组。至于您的示例,如果我想复制第 1、2 和 4 列怎么办?这是一个太具体的用例,迭代非常简单。这里没有真正需要解决的问题。