0

我有一个时间表条目如下

活动代码:持续时间


  • 项目 A:12 小时 31 分钟
  • 项目 B: 00h:10mins
  • 项目A:01h:10mins
  • 项目 A:12 小时 31 分钟
  • 项目 C:12 小时 31 分钟
  • 项目 B: 00h:10mins
  • 项目 B:01h:10mins
  • 项目 A:12 小时 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,
                      };
4

2 回答 2

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,
                  };
于 2013-03-16T02:04:49.090 回答
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
                            };
于 2013-03-16T05:21:46.400 回答