C#-具有动态列数的Linq数据透视表

时间:2020-04-28 12:59:27

标签: c# linq group-by pivot

我想用动态的列数来透视列表。到目前为止,我已经找到了几个示例,但是我不知道如何将它们应用于我的问题。 使用以下内容,我得到一个基于Dev.DevType的值的列表:

using (var context = new TableEntities())
{
    var result = (from Dev in context.Device
                  from Mod in context.Module.Where(m => Dev.ModId == m.Id && Dev.SecId == m.SecId).DefaultIfEmpty()
                  from DevT in context.DevType.Where(d => Dev.DevType == d.DevType).DefaultIfEmpty()
                  where Dev.DevType == 10
                  select new temp
                  {
                      DevName = Dev.DevName,
                      DevSuffix = DevT.Suffix,
                      Name = String.Concat(Mod.ModName, "_", Dev.DevName, "_", DevT.Suffix),
                  }).ToList();
}

根据Dev.DevType的值,我可以有不同的输出,例如:

Dev.DevType == 10:

DevName     DevSuffix   Name
Enter       A           Door_Enter_A
Enter       Text        Door_Enter_Text
Enter       C           Door_Enter_C
Hall        A           Door_Hall_A
Hall        Text        Door_Hall_Text
Hall        C           Door_Hall_C

但这也可能是这样的:

Dev.DevType == 15:

DevName     DevSuffix   Name
Attic       A           Window_Attic_A
Attic       B           Window_Attic_B
Attic       Purple      Window_Attic_Purple
Attic       D           Window_Attic_D
Attic       E           Window_Attic_E
Kitchen     A           Window_Kitchen_A
Kitchen     B           Window_Kitchen_B
Kitchen     Purple      Window_Kitchen_Purple
Kitchen     D           Window_Kitchen_D
Kitchen     E           Window_Kitchen_E
Dining      A           Window_Dining_A
Dining      B           Window_Dining_B
Dining      Purple      Window_Dining_Purple
Dining      D           Window_Dining_D
Dining      E           Window_Dining_E

我想对此进行透视,以便我有基于DevSuffix的列,并且行包含Name,所以:

使用Dev.DevType == 10:

A               Text            C
Door_Enter_A    Door_Enter_Text Door_Enter_C
Door_Hall_A     Door_Hall_Text  Door_Hall_C

Dev.DevType == 15:

A                   B                   Purple                D                 E
Window_Attic_A      Window_Attic_B      Window_Attic_Purple   Window_Attic_D    Window_Attic_E
Window_Kitchen_A    Window_Kitchen_B    Window_Kitchen_Purple Window_Kitchen_D  Window_Kitchen_E
Window_Dining_A     Window_Dining_B     Window_Dining_Purple  Window_Dining_D   Window_Dining_E

我发现了几个使用GroupBy()GroupJoin()的示例,但我唯一完成的事情是:

.GroupBy(d => d.DevName, (Name) => new
{
    Key = DevName,
    Name = Name,
}).ToList();

这将导致list中的list看起来像:

使用Dev.DevType == 10:

DevSuffix   Name
A           Door_Enter_A
            Door_Hall_A
Text        Door_Enter_Text
            Door_Hall_Text
C           Door_Enter_C            
            Door_Hall_C

Dev.DevType == 15:

DevSuffix   Name
A           Window_Attic_A
            Window_Kitchen_A
            Window_Dining_A
B           Window_Attic_B
            Window_Kitchen_B
            Window_Dining_B
Purple      Window_Attic_Purple
            Window_Kitchen_Purple
            Window_Dining_Purple
D           Window_Attic_D
            Window_Kitchen_D
            Window_Dining_D
E           Window_Attic_E
            Window_Kitchen_E
            Window_Dining_E

我不知道从这里去哪里,所以我们将不胜感激。预先谢谢你!

0 个答案:

没有答案