复制数据表中的两列

发布于 2024-12-18 02:23:40 字数 1069 浏览 1 评论 0原文

我有一个问题: 我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧竹 2024-12-25 02:23:40

假设 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);
}

Assuming that the both the TicketPrice and IndexPrice tables match up then you could do something like:

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);
}
深居我梦 2024-12-25 02:23:40

您可以将 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.Rows 和 IndexPrice.Rows 中的值对齐。

Can you just take the NewRow() and Rows.Add() out of the inner loop? Something like this:

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);

This assumes the values in TickerPrice.Rows and IndexPrice.Rows line up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文