C#LINQ聚合存在列表

时间:2017-10-22 17:27:37

标签: c# linq aggregate

我有一个这样的课程:

     public class UserBadge {
         public int Id {get;set;}
         public string Name { get; set; }
         public string Surname { get; set; }
         public string FiscalCode { get; set; }
         public int Hours { get; set; }
         public int Day { get; set; }
         public int Month { get; set; }
         public int Year { get; set; }
      }

我填写这些数据:

var listUserBadge = new List<UserBadge>;
listUserBadge .Add(new UserBadge { Id = 1,
                                   Name = 'Mario',
                                   Surname = 'Rossi',
                                   FiscalCode = 'XXXX',
                                   Hours = 8,
                                   Month = 1,
                                   Year = 2017,
                                   Day = 4
                    });
listUserBadge .Add(new UserBadge { Id = 1,
                                   Name = 'Mario',
                                   Surname = 'Rossi',
                                   FiscalCode = 'XXXX',
                                   Hours = 4,
                                   Month = 2,
                                   Year = 2017,
                                   Day = 4
                    });
listUserBadge .Add(new UserBadge { Id = 1,
                                   Name = 'Mario',
                                   Surname = 'Rossi',
                                   FiscalCode = 'XXXX',
                                   Hours = 8,
                                   Month = 1,
                                   Year = 2017,
                                   Day = 10
                    });
listUserBadge .Add(new UserBadge { Id = 2,
                                   Name = 'Pippo',
                                   Surname = 'Pluto',
                                   FiscalCode = 'XXXX',
                                   Hours = 8,
                                   Month = 1,
                                   Year = 2017,
                                   Day = 4
                    });

我想要一个linq命令用于聚合相同的ID,月份和年份(不关心日期)总计小时数,因此要对现有数据进行某些操作,以便以这种方式添加数据:

var listUserBadge = new List<UserBadge>;
listUserBadge .Add(new UserBadge { Id = 1,
                                   Name = 'Mario',
                                   Surname = 'Rossi',
                                   FiscalCode = 'XXXX',
                                   Hours = 16,
                                   Month = 1,
                                   Year = 2017,
                                   Day = 0
                    });
listUserBadge .Add(new UserBadge { Id = 1,
                                   Name = 'Mario',
                                   Surname = 'Rossi',
                                   FiscalCode = 'XXXX',
                                   Hours = 4,
                                   Month = 2,
                                   Year = 2017,
                                   Day = 4
                    });
listUserBadge .Add(new UserBadge { Id = 2,
                                   Name = 'Pippo',
                                   Surname = 'Pluto',
                                   FiscalCode = 'XXXX',
                                   Hours = 8,
                                   Month = 1,
                                   Year = 2017,
                                   Day = 4
                    });

之后我将迭代每个数据并提取单个.CSV,但这是另一个故事。我不明白该怎么做,有人能帮帮我吗?

由于

1 个答案:

答案 0 :(得分:1)

您可以使用import { Welcome } from '../pages/welcome/welcome'; @NgModule({ declarations: [ MyApp, HomePage, ListPage, Welcome ... 执行此操作,您可以为此提供密钥并将小时数相加。

GroupBy