您好我有以下需要旋转的表格以获得所需的结果。
tblUsername
userId username
---------------------
1 jadam
2 janeb
3 peterk
4 rickd
第二张表是
tablcolumns
=============
colId colname
-----------------
1 Name
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
6 Friday
7 Saturday
8 Sunday
第三表
tblSchedule
==================
id userId colId hrs
--------------------------
1 2 3 4
2 3 7 7.5
3 1 8 2
I am trying to write Linq statem
我想透过这些表来获得回复,如
Name Monday Tuesday Wednesday Thursday Friday Saturday Sunday
jadam 2
janeb 4
peterk 7.5
rickd
我尝试使用Linq加入,但它没有工作我相信它需要分组,我不太熟悉它。谢谢
答案 0 :(得分:0)
LINQ中没有内置的透视功能。我想你可以写一个动态创建选择 - 但是,它可能不会太高效。无论如何,对于这种情况,由于每周的天数不会改变,你可以这样做:
var query = tblUsername.Select(u => new {
Name = u.username,
Monday = u.tblSchedule.Where(sch => sch.colId == 2).Sum(sch => sch.hrs),
Tuesday = u.tblSchedule.Where(sch => sch.colId == 3).Sum(sch => sch.hrs),
Wednesday = u.tblSchedule.Where(sch => sch.colId == 4).Sum(sch => sch.hrs),
Thursday = u.tblSchedule.Where(sch => sch.colId == 5).Sum(sch => sch.hrs),
Friday = u.tblSchedule.Where(sch => sch.colId == 6).Sum(sch => sch.hrs),
Saturday = u.tblSchedule.Where(sch => sch.colId == 7).Sum(sch => sch.hrs),
Sunday = u.tblSchedule.Where(sch => sch.colId == 8).Sum(sch => sch.hrs),
});