Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多列到行_C#_Asp.net_Linq_Lambda - Fatal编程技术网

C# 多列到行

C# 多列到行,c#,asp.net,linq,lambda,C#,Asp.net,Linq,Lambda,根据第一个图像,我在datatable中有数据,但我想转换我的datatable或创建一个新的datatable,并在第二个图像中有新的输出 var reportData = (from C in Table1 join LC in Table2 on C.Id equals LC.Id select new { C.Id, C.



根据第一个图像,我在datatable中有数据,但我想转换我的datatable或创建一个新的datatable,并在第二个图像中有新的输出

                var reportData = (from C in Table1
                                  join LC in Table2 on C.Id equals LC.Id
                                  select new { C.Id, C.User, LC.Phy, LC.Che, LC.Bio, LC.Math }).ToList();

                foreach (var item in reportData)
                {

                    lstLc.Add(new Launch() { Username= item.User, Phy = item.Phy, Che = item.Che, Bio = item.Bio, Math = item.Math });
        }
原始数据:

所需的输出

User    Type    value
A       Phy     12
A       Che     20
A       Bio     16
A       Math    10
B       Phy     15
B       Che     19
B       Bio     18
B       Math    20
C       Phy     13
C       Che     17
C       Bio     11
C       Math    18

您可以像这样使用
union

SELECT User, Type, value FROM (
    SELECT User, 'Phy' AS 'Type', Phy AS 'value' FROM table1
    UNION
    SELECT User, 'Che' AS 'Type', Che AS 'value' FROM table1
    UNION
    SELECT User, 'Bio' AS 'Type', Bio AS 'value' FROM table1
    UNION
    SELECT User, 'Math' AS 'Type', Math AS 'value' FROM table1
) AS t
ORDER BY User
var list = entity.Select(e => new { User = e.User, Type = "Phy", value = e.Phy })
    .Union(entity.Select(e => new { User = e.User, Type = "Che", value = e.Che }))
    .Union(entity.Select(e => new { User = e.User, Type = "Bio", value = e.Bio }))
    .Union(entity.Select(e => new { User = e.User, Type = "Math", value = e.Math }))
    .OrderBy(e => e.User);
在LINQ中,您可以执行以下操作:

SELECT User, Type, value FROM (
    SELECT User, 'Phy' AS 'Type', Phy AS 'value' FROM table1
    UNION
    SELECT User, 'Che' AS 'Type', Che AS 'value' FROM table1
    UNION
    SELECT User, 'Bio' AS 'Type', Bio AS 'value' FROM table1
    UNION
    SELECT User, 'Math' AS 'Type', Math AS 'value' FROM table1
) AS t
ORDER BY User
var list = entity.Select(e => new { User = e.User, Type = "Phy", value = e.Phy })
    .Union(entity.Select(e => new { User = e.User, Type = "Che", value = e.Che }))
    .Union(entity.Select(e => new { User = e.User, Type = "Bio", value = e.Bio }))
    .Union(entity.Select(e => new { User = e.User, Type = "Math", value = e.Math }))
    .OrderBy(e => e.User);
编辑将代码更改为:

var repFlatData = (from C in Table1
                   join LC in Table2 on C.Id equals LC.Id
                   select new { C.Id, C.User, LC.Phy, LC.Che, LC.Bio, LC.Math }).ToList();

var reportData = repFlatData.Select(e => new { User = e.User, Type = "Phy", value = e.Phy })
          .Union(repFlatData.Select(e => new { User = e.User, Type = "Che", value = e.Che }))
          .Union(repFlatData.Select(e => new { User = e.User, Type = "Bio", value = e.Bio }))
          .Union(repFlatData.Select(e => new { User = e.User, Type = "Math", value = e.Math }))
          .OrderBy(e => e.User);

foreach (var item in reportData) {
    lstLc.Add(new Launch() { Username= item.User, Type = item.Type, Value = item.Value });
}

当然,您需要将
Launch
类更改为只包含列(用户名、类型、值).

问题是什么?根据第一个图像,我的数据表中有数据,但我想转换我的数据表或创建一个新的数据表,新的输出在第二个图像中。您是否理解这是一个请求,而不是问题的问题?不仅如此,您甚至没有提供一些代码来使用。数据在哪里?我希望在ASP.net中使用Lambda查询,而不是在SQL.Thnks Racil中。我已经编辑了我的问题,并用C#编写了实际的代码。那么你能帮我写同样的代码吗。由于我是Asp.net新手,请参见我编辑的答案。Thnx很多Racil。。它现在起作用了。。。