如何根据某些条件创建重复行?

发布于 2024-12-01 00:13:30 字数 442 浏览 0 评论 0原文

我的数据表有一些列包含格式为 2011~|~2012~|~2013 的数据。现在表只有一行。但根据此类数据的数量,应创建相应的行。

因此,在上述格式中,应创建三行,每行的年份列填写为 2011、2012 和 2013。如果其他列已填满,也没关系。重复

类似于“年份”列的格式还存在另外 2-3 个列,例如“销售”列将数字表示为 190~|~250~|~488。

重要提示:此处的行数应为 3(考虑年份和销售额列)。它不应该是 9 (3 x 3)。存在一对一的关系,比如2011年销售额为1.9亿美元,2012年销售额为2.5亿美元,2013年预计销售额为4.88亿美元。

下图更详细地解释了。

我猜眼底已经清理干净了。如果没有,请告诉我。

请指导

图片已添加

My Datatable has some columns that contains data in the format as 2011~|~2012~|~2013. Now the table has only one row. But based on how many such data is there, that many rows should be created

Hence, in the above format, three rows should be created each having Year column filled as 2011, 2012 and 2013. It doesn't matter that if other columns are being duplicated

Format similar to Year column also exists for some 2-3 more columns such as Sales column states figures as 190~|~250~|~488.

One Important Note: The number of rows here should be 3 (considering year & sales column). It should not be 9 (3 x 3). There is a one-to-one relationship, say in year 2011 sales was 190mn$, for 2012 it was 250 mn$ and for 2013 expected is 488mn$.

The below image explains more in detail.

I guess the funda is cleared. If not, let me know.

Please guide

Image Added

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不可一世的女人 2024-12-08 00:13:30

您在寻找这样的东西吗?

private DataTable AlterDataTable(DataTable oldTable)
{
    DataTable newTable = oldTable.Clone();
    foreach (DataRow row in oldTable.Rows)
    {
        var years = row["Year"]
        .ToString()
        .Split(new string[] { "~|~" }, StringSplitOptions.RemoveEmptyEntries)
        .ToList();
        foreach (var year in years)
        {
            row["Year"] = year;
            newTable.ImportRow(row);
        }

    }

    return newTable;
}

将其称为 DataTable dataTable = AlterDataTable(dt);

对于此要求,请尝试此操作

private DataTable AlterDataTable(DataTable oldTable)
{
    DataTable newTable = oldTable.Clone();
    foreach (DataRow row in oldTable.Rows)
    {
        var years = row["Year"]
        .ToString()
        .Split(new string[] { "~|~" }, StringSplitOptions.RemoveEmptyEntries)
        .ToList();
        var sales = row["Sales"]
        .ToString()
        .Split(new string[] { "~|~" }, StringSplitOptions.RemoveEmptyEntries)
        .ToList();
        if (years.Count != sales.Count)
        {
            throw new Exception("Argument count mismatch exception");
        }
        for (var i = 0; i < years.Count; i++)
        {
            row["Year"] = years[i];
            row["Sales"] = sales[i];
            newTable.ImportRow(row);
        }
    }
    return newTable;
}

请注意,拆分应具有相同的计数。

Are you looking for something like this?

private DataTable AlterDataTable(DataTable oldTable)
{
    DataTable newTable = oldTable.Clone();
    foreach (DataRow row in oldTable.Rows)
    {
        var years = row["Year"]
        .ToString()
        .Split(new string[] { "~|~" }, StringSplitOptions.RemoveEmptyEntries)
        .ToList();
        foreach (var year in years)
        {
            row["Year"] = year;
            newTable.ImportRow(row);
        }

    }

    return newTable;
}

Call it as DataTable dataTable = AlterDataTable(dt);

For this requirement, try this

private DataTable AlterDataTable(DataTable oldTable)
{
    DataTable newTable = oldTable.Clone();
    foreach (DataRow row in oldTable.Rows)
    {
        var years = row["Year"]
        .ToString()
        .Split(new string[] { "~|~" }, StringSplitOptions.RemoveEmptyEntries)
        .ToList();
        var sales = row["Sales"]
        .ToString()
        .Split(new string[] { "~|~" }, StringSplitOptions.RemoveEmptyEntries)
        .ToList();
        if (years.Count != sales.Count)
        {
            throw new Exception("Argument count mismatch exception");
        }
        for (var i = 0; i < years.Count; i++)
        {
            row["Year"] = years[i];
            row["Sales"] = sales[i];
            newTable.ImportRow(row);
        }
    }
    return newTable;
}

Please note that the splits should be having the same count.

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