复制数据表中的两列
本文关键字:两列 数据表 复制 | 更新日期: 2023-09-27 18:15:27
我有一个c#问题:我有2个数据表A和B,它们都包含一个名为"移动"的列,我想创建另一个有2列的数据表,一个从A"移动"另一个从B"移动",我尝试了这样的东西:
//cherche les last price
DataTable TickerPrice = new DataTable("Data");
TickerPrice = CheckBloomi(TickerName + " equity", "CHG_PCT_1D", FromThisTime, ToThisTime);
//cherche les last price
DataTable IndexPrice = new DataTable("Data");
IndexPrice = CheckBloomi("E300 Index", "CHG_PCT_1D", FromThisTime, ToThisTime);
DataSet MarketData = new DataSet();
DataTable Recap = MarketData.Tables.Add("Recap");
Recap.Columns.Add("Move Ticker price");
Recap.Columns.Add("Move Index price");
foreach (DataRow sourcerow in TickerPrice.Rows)
{
DataRow destRow = Recap.NewRow();
destRow["Move Ticker price"] = sourcerow["CHG_PCT_1D"];
Recap.Rows.Add(destRow);
}
foreach (DataRow sourcerow in IndexPrice.Rows)
{
DataRow destRow = Recap.NewRow();
destRow["Move Index price"] = sourcerow["CHG_PCT_1D"];
Recap.Rows.Add(destRow);
}
这个工作很好地复制一列(对于第一个foreach循环),但是对于第二列,我有数字移位,因为我正在重新创建新的行。
你知道怎么做吗?如果不够清楚,请告诉我
假设TicketPrice和IndexPrice表都匹配,那么您可以这样做:
for (int i = 0; i < TickerPrice.Rows.Count; i++)
{
DataRow destRow = Recap.NewRow();
destRow["Move Ticker price"] = TickerPrice.Rows[i]["CHG_PCT_1D"];
destRow["Move Index price"] = IndexPrice.Rows[i]["CHG_PCT_1D"];
Recap.Rows.Add(destRow);
}
你能把NewRow()
和Rows.Add()
从内循环中取出来吗?像这样:
DataRow destRow = Recap.NewRow();
foreach (DataRow sourcerow in TickerPrice.Rows)
{
destRow["Move Ticker price"] = sourcerow["CHG_PCT_1D"];
}
foreach (DataRow sourcerow in IndexPrice.Rows)
{
destRow["Move Index price"] = sourcerow["CHG_PCT_1D"];
}
Recap.Rows.Add(destRow);
假设TickerPrice中的值。行和IndexPrice。行对齐