使用LINQ进行分组和聚合

时间:2013-03-16 01:52:16

标签: c# linq

我有一个时间表条目如下

活动代码:持续时间


  • 项目A:12h:31分钟
  • 项目B:00h:10分钟
  • 项目A:01h:10分钟
  • 项目A:12h:31分钟
  • 项目C:12h:31分钟
  • 项目B:00h:10分钟
  • 项目B:01h:10分钟
  • 项目A:12h:31分钟

对项目进行分组汇总的总时间是什么?我正在尝试下面的

    var summary = from entry in DbSet
                  where entry.Timesheet.UserID == userid &&
                  entry.Timesheet.DateSubmitted >= startdate &&
                  entry.Timesheet.DateSubmitted <= enddate
                  group entry by entry.ActivityCode.ActivityCode1
                      into groupEntry
                      select new TimeSheetSummary()
                      {
                          ActivityCode = groupEntry.Key,
                          HourSpent = Convert.ToInt32(groupEntry.Sum(x => x.Duration)),
                          Percentage = (Convert.ToInt32(groupEntry.Sum(x => x.Duration)) / 8) * 100,
                          MinuteSpent = Convert.ToInt32(groupEntry.Sum(x => x.Duration)) * 60,
                      };

2 个答案:

答案 0 :(得分:1)

如果您想获得一个百分比,请计算一下活动的数量并使用它来除以。

此处不确定是否需要除以0。如果没有数据可以开始,我不确定第二个LINQ会做什么。不确定是否会引发错误。

Int32 numberOfProjects = (from entry in DbSet
              where entry.Timesheet.UserID == userid &&
                  entry.Timesheet.DateSubmitted >= startdate &&
                  entry.Timesheet.DateSubmitted <= enddate
              select entry.ActivityCode.ActivityCode1).Distinct().Count();

var summary = from entry in DbSet
              where entry.Timesheet.UserID == userid &&
              entry.Timesheet.DateSubmitted >= startdate &&
              entry.Timesheet.DateSubmitted <= enddate
              group entry by entry.ActivityCode.ActivityCode1
                  into groupEntry
                  select new TimeSheetSummary()
                  {
                      ActivityCode = groupEntry.Key,
                      HourSpent = Convert.ToInt32(groupEntry.Sum(x => x.Duration)),
                      Percentage = (Convert.ToInt32(groupEntry.Sum(x => x.Duration)) / numberOfProjects) * 100,
                      MinuteSpent = Convert.ToInt32(groupEntry.Sum(x => x.Duration)) * 60,
                  };

答案 1 :(得分:0)

这是我根据线程中其他人的输入找到的答案。谢谢!

    var groupEntries = from entry in DbSet
                       where entry.Timesheet.UserID == userId &&
                             entry.Timesheet.TimeSheetDate <= endDate.Date &&
                             entry.Timesheet.TimeSheetDate >= startDate.Date
                       group entry by entry.ActivityCode
                           into groupEntry
                           select new
                           {
                               ActivityCode = groupEntry.Key,
                               Duration = Convert.ToInt16(groupEntry.Sum(r => r.Duration))
                           };

    var totalDuration = groupEntries.Sum(r => r.Duration);

    var result = from groupEntry in groupEntries
                 select new TimeSheetSummary()
                            {
                                ActivityCode = groupEntry.ActivityCode,
                                HourSpent = groupEntry.Duration / 60,
                                MinuteSpent = groupEntry.Duration % 60,
                                Percentage = groupEntry.Duration / totalDuration * 100
                            };