如何使用 lambda 表达式或使用任何其他逻辑转换数据?

发布于 2024-11-15 18:15:24 字数 894 浏览 3 评论 0原文

我有一个数据表,其中包含这种格式的数据。我想将其转换为另一种格式。 实现这一目标的最简单方法是什么?也许使用 lambda 表达式。我正在使用 C# 4.0

dattable1:
    seq     id        Amt
=============================
    1     00782       10 
    2     00782       20
    3     00782       30

    1     003850      40
    2     003850      50
    3     003850      60

    1     005723      70
    2     005723      80
    3     005723      90


To be stored in another datatable or list in this format

1  00782  003850  005723  10  40  70
2  00782  003850  005723  20  50  80
3  00782  003850  005723  30  60  90

示例 2

 dattable1:
        seq     id        Amt
    =============================
        1     00782       10 


        1     003850      40


        1     005723      70



    To be stored in another datatable or list in this format

    1  00782  003850  005723  10  40  70

I have a DataTable which contains data in this format. I want to convert it to another format.
What is the simplest way to achieve this? maybe using lambda expression. I am using
C# 4.0

dattable1:
    seq     id        Amt
=============================
    1     00782       10 
    2     00782       20
    3     00782       30

    1     003850      40
    2     003850      50
    3     003850      60

    1     005723      70
    2     005723      80
    3     005723      90


To be stored in another datatable or list in this format

1  00782  003850  005723  10  40  70
2  00782  003850  005723  20  50  80
3  00782  003850  005723  30  60  90

example 2

 dattable1:
        seq     id        Amt
    =============================
        1     00782       10 


        1     003850      40


        1     005723      70



    To be stored in another datatable or list in this format

    1  00782  003850  005723  10  40  70

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

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

发布评论

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

评论(1

狼亦尘 2024-11-22 18:15:24

这将生成与您要求的内容相匹配的输入。

var datatable1 = new[]
{
    new { seq = 1, id = "00782", Amt = 10 }, 
    new { seq = 2, id = "00782", Amt = 20 }, 
    new { seq = 3, id = "00782", Amt = 30 }, 
    new { seq = 1, id = "003850", Amt = 40 }, 
    new { seq = 2, id = "003850", Amt = 50 }, 
    new { seq = 3, id = "003850", Amt = 60 }, 
    new { seq = 1, id = "005723", Amt = 70 }, 
    new { seq = 2, id = "005723", Amt = 80 }, 
    new { seq = 3, id = "005723", Amt = 90 }
};


var result = datatable1
    .GroupBy(arg => arg.seq)
    .Select(arg =>
        new
        {
            arg.Key,
            id1 = "00782",
            id2 = "003850",
            id3 = "005723",
            Amt1 = arg.Where(x => x.id == "00782").Sum(x => x.Amt),
            Amt2 = arg.Where(x => x.id == "003850").Sum(x => x.Amt),
            Amt3 = arg.Where(x => x.id == "005723").Sum(x => x.Amt),
        })
    .ToList();

[编辑]

对于“所有场景”:

var result = datatable1
    .GroupBy(arg => arg.seq)
    .Select(arg =>
        new
        {
            arg.Key,
            Ids = arg.Select(x => x.id).ToList(),
            Amounts = arg.Select(x => x.Amt).ToList(),
        })
    .ToList();

foreach (var row in result)
    Console.WriteLine("{0}\t{1}\t{2}", row.Key, string.Join("\t", row.Ids), string.Join("\t", row.Amounts));

输出:

1       00782   003850  005723  10      40      70
2       00782   003850  005723  20      50      80
3       00782   003850  005723  30      60      90

This will generate the input that matches what you asked for.

var datatable1 = new[]
{
    new { seq = 1, id = "00782", Amt = 10 }, 
    new { seq = 2, id = "00782", Amt = 20 }, 
    new { seq = 3, id = "00782", Amt = 30 }, 
    new { seq = 1, id = "003850", Amt = 40 }, 
    new { seq = 2, id = "003850", Amt = 50 }, 
    new { seq = 3, id = "003850", Amt = 60 }, 
    new { seq = 1, id = "005723", Amt = 70 }, 
    new { seq = 2, id = "005723", Amt = 80 }, 
    new { seq = 3, id = "005723", Amt = 90 }
};


var result = datatable1
    .GroupBy(arg => arg.seq)
    .Select(arg =>
        new
        {
            arg.Key,
            id1 = "00782",
            id2 = "003850",
            id3 = "005723",
            Amt1 = arg.Where(x => x.id == "00782").Sum(x => x.Amt),
            Amt2 = arg.Where(x => x.id == "003850").Sum(x => x.Amt),
            Amt3 = arg.Where(x => x.id == "005723").Sum(x => x.Amt),
        })
    .ToList();

[Edit]

For "all scenarios":

var result = datatable1
    .GroupBy(arg => arg.seq)
    .Select(arg =>
        new
        {
            arg.Key,
            Ids = arg.Select(x => x.id).ToList(),
            Amounts = arg.Select(x => x.Amt).ToList(),
        })
    .ToList();

foreach (var row in result)
    Console.WriteLine("{0}\t{1}\t{2}", row.Key, string.Join("\t", row.Ids), string.Join("\t", row.Amounts));

Output:

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