复制数据表中的两列
我有一个问题: 我有 2 个数据表 A 和 B,它们都包含一个名为“move”的列,我想创建另一个包含 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 循环),但对于第二列,我将数字转移,因为我正在重新创建新行。
您知道如何做到这一点吗?如果不够清楚,请告诉我
I have a c# question :
I have 2 datatable A and B both of them contain a column called “move” , I want to create another Datable with 2 columns one with “move” from A the other with “move” from B , I tried something like this :
//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);
}
This work fine to copy one column (for the first foreach loop) but then for the second column I have the number shifted because I am recreating new rows.
Do you an idea of how to do that ?, let me know if it is not clear enough
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设 TicketPrice 和 IndexPrice 表都匹配,那么您可以执行以下操作:
Assuming that the both the TicketPrice and IndexPrice tables match up then you could do something like:
您可以将
NewRow()
和Rows.Add()
从内部循环中取出吗?像这样的事情:这假设 TickerPrice.Rows 和 IndexPrice.Rows 中的值对齐。
Can you just take the
NewRow()
andRows.Add()
out of the inner loop? Something like this:This assumes the values in TickerPrice.Rows and IndexPrice.Rows line up.