计算一周的总加班时间

Calculating Total Overtime for the week

谁能帮帮我,所以我想做一个方法,从加班表中计算出每个月每个星期的总小时数。

readonly List<KeyValuePair<DateTime, TimeSpan>> overTimeList = new List<KeyValuePair<DateTime, TimeSpan>>();//the list of overtime hours, the data is printed below in the form of data {...}

DateTime start = new DateTime(calendar1.CurrentDate.Year, calendar1.CurrentDate.Month, 1); //start (beginning of month in calendar)
DateTime stop = new DateTime(calendar1.CurrentDate.Year, calendar1.CurrentDate.Month, 1).AddMonths(1).AddDays(-1);//end (end of calendar month)
var weeksOverTimeSum = WeeklyOverTime(start, stop); //This is how I call the method
//method in which I want to get a certain list
private List<SundayWithWeekSum> WeeklyOverTime(DateTime startDate, DateTime stopDate)
        {
            var w = (int)startDate.DayOfWeek - 1;
            var result = from q in overTimeList
                         where q.Key >= startDate && q.Key <= stopDate
                         group q by (q.Key.Date.Day + w) / 7 into g
                         select new SundayWithWeekSum { Day = g.FirstOrDefault().Key, Balance = new TimeSpan(g.Sum(x => x.Value.Ticks)) };
                        //group q by(q.Key.Date.Day + (w == 0 ? w - 1 : w - 2)) / 7 into g
                        //select new SundayWithWeekSum { Day = g.FirstOrDefault().Key, Balance = new TimeSpan(g.Sum(x => x.Value.Ticks)) };
            return result.ToList();
        }

    class SundayWithWeekSum
    {
    public DateTime Day { get; set; }
    public TimeSpan Balance { get; set; }
    }

我在日历中显示这些数据,我会考虑 6 月份,列表是几天的加班时间,我想要整个月的 overTimeList

data {
[0]{6/3/2019, 00:12:00},
[1]{6/4/2019, -00:11:00},
[2]{6/5/2019, 00:13:00},
[3]{6/7/2019, 00:13:00},
[4]{6/8/2019, -00:14:00},
[5]{6/11/2019, -00:35:00},
[6]{6/12/2019, 00:15:00},
[7]{6/14/2019, -00:02:00},
[8]{6/15/2019, -01:20:00},
[9]{6/17/2019,  00:11:00},
[10]{6/18/2019, 00:08:00},
[11]{6/19/2019, 00:14:00},
[12]{6/21/2019, 00:14:00},
[13]{6/22/2019, -00:54:00},
[14]{6/24/2019, 00:20:00},
[15]{6/25/2019, 00:12:00},
[16]{6/26/2019, 00:12:00},
[0]{6/29/2019, 00:48:00}
}

所以是时候在日历中显示工人的工作时间了,现在,作为方法的输入,我指定了特定月份的开始和停止,然后我想计算总的工作量一个月的所有星期都加班。您终于获得了 SundayWithWeekSum 列表,例如:

FirstWeek- 06/02/2019; +00.00, 
SecondWeek- 06/09/2019; 13.00, 
ThirdWeek- 16/06/2019; -1.47, 
FourthWeek- 06/23/2019; -00.27, 
FifthWeek- 06/30/2019; 1.12

这是我的代码示例,我以为我正在经历一个月,每天,并在周日完成时休息一下,我将每周(天)的总余额保存在列表 "balanceForWeeks".

感谢大家的帮助。

这应该可以解决问题 -

readonly List<KeyValuePair<DateTime, TimeSpan>> overTimeList = new List<KeyValuePair<DateTime, TimeSpan>>();
private List<SundayWithWeekSum> WeeklyOverTime(DateTime startDate, DateTime stopDate)
{
     var w = (int)startDate.DayOfWeek - 1;
     var result = from q in overTimeList
                  where q.Key >= startDate && q.Key <= stopDate
                  group q by (q.Key.Date.Day + w) / 7 into g
                  select new SundayWithWeekSum { Day = g.FirstOrDefault().Key, Balance = new TimeSpan(g.Sum(x => x.Value.Ticks)) };
    return result.ToList();
}

以上函数将 return List<SundayWithWeekSum>以星期日开始的一周和一周的总持续时间。它包括开始日期和结束日期。

如果我遗漏了什么,请告诉我。