I would like to ask for help i'm having trouble with Min and Max the time in and time out of employees.
The thing is employees need to swipe their cards to enter and exit the property so they will have multiple swipe in and out during their whole shift. Below is my code trying to figure out how to get the min and max time of entry and exit of the employee.
TimeSpan minDuration = TimeSpan.Parse("00:01"); //minimum duration from entry to exit
TimeSpan maxDuration = TimeSpan.Parse("14:00"); //maximum duration from entry to exit
DateTime fromDate = DateTime.Parse("2021-1-10"); //From date range filter
DateTime toDate = DateTime.Parse("2021-1-12"); //To date range filter
List<StaffComplianceList> setStaffComplianceList = new List<StaffComplianceList>();
setStaffComplianceList.Add(new StaffComplianceList { EmployeeNumber = "2448", CardholderName = "Aziel Cai", OccurrenceTime = new DateTime(2021, 01, 10, 05, 24, 09), EventSource = "Staff Entrance Lane 3 Entry", EventType = "Door Access Granted" });
setStaffComplianceList.Add(new StaffComplianceList { EmployeeNumber = "2448", CardholderName = "Aziel Cai", OccurrenceTime = new DateTime(2021, 01, 10, 06, 13, 54), EventSource = "Staff Entrance Lane 7 Exit", EventType = "Card Exit Granted" });
setStaffComplianceList.Add(new StaffComplianceList { EmployeeNumber = "2448", CardholderName = "Aziel Cai", OccurrenceTime = new DateTime(2021, 01, 10, 06, 54, 34), EventSource = "Staff Entrance Lane 4 Entry", EventType = "Door Access Granted" });
setStaffComplianceList.Add(new StaffComplianceList { EmployeeNumber = "2448", CardholderName = "Aziel Cai", OccurrenceTime = new DateTime(2021, 01, 10, 14, 03, 24), EventSource = "Staff Entrance Lane 5 Exit", EventType = "Card Exit Granted" });
setStaffComplianceList.Add(new StaffComplianceList { EmployeeNumber = "2448", CardholderName = "Aziel Cai", OccurrenceTime = new DateTime(2021, 01, 11, 18, 25, 09), EventSource = "Staff Entrance Lane 3 Entry", EventType = "Door Access Granted" });
setStaffComplianceList.Add(new StaffComplianceList { EmployeeNumber = "2448", CardholderName = "Aziel Cai", OccurrenceTime = new DateTime(2021, 01, 11, 19, 00, 04), EventSource = "Staff Entrance Lane 7 Exit", EventType = "Card Exit Granted" });
setStaffComplianceList.Add(new StaffComplianceList { EmployeeNumber = "2448", CardholderName = "Aziel Cai", OccurrenceTime = new DateTime(2021, 01, 11, 19, 24, 34), EventSource = "Staff Entrance Lane 4 Entry", EventType = "Door Access Granted" });
setStaffComplianceList.Add(new StaffComplianceList { EmployeeNumber = "2448", CardholderName = "Aziel Cai", OccurrenceTime = new DateTime(2021, 01, 12, 07, 03, 24), EventSource = "Staff Entrance Lane 5 Exit", EventType = "Card Exit Granted" });
//Filter the list base on the date range value
var staffList = setStaffComplianceList.Where(x => (x.OccurrenceTime.Date >= fromDate) && (x.OccurrenceTime.Date <= toDate)).ToList();
var getMinAndMax = from stafflist in staffList
group stafflist by new { stafflist.OccurrenceTime.Date , stafflist.EmployeeNumber} into staffComplianceGroup
let f = staffComplianceGroup.FirstOrDefault()
select new
{
EmployeeNumber = f.EmployeeNumber,
CardholderName = f.CardholderName,
MinTime = staffComplianceGroup.Min(x => x.OccurrenceTime),
MaxTime = staffComplianceGroup.Max(x => x.OccurrenceTime),
};
What I'm trying to accomplish is to monitor employees if they use the company staff entry point and exit point during their shift.
They can have multiple entry and exit for their whole shift due to the smoking area is outside the entry and exit.
I have a minDuration
and maxDuration
TimeSpan
, what it does is user can filter the minimum duration and the maximum duration of employee before they exit the property to be tagged as compliant, From 1 minute to 14 hours I will capture their in and out it means including the smoking breaks will be captured. But those in and outs during their breaks is not important, what I need is the first entry and last so I can tagged them as compliant using the entry and exit point.
fromDate and toDate is filter of date range to process. I hope i clarify what i'm trying to accomplish.
question from:
https://stackoverflow.com/questions/65852958/c-sharp-get-the-min-and-max-datetime-of-employee