public int HSCodeOutputPerPerHour(int userId) { List <HSCodeAvgOutput> list = new List <HSCodeAvgOutput>(); try { // get all operators of the manager var operators = (from r in dbContext.Users join u in dbContext.UserAdditionals on r.UserId equals u.UserId join t in dbContext.Timezones on r.TimezoneId equals t.TimezoneId where u.ManagerUserId == userId && r.IsActive == true select new { OperatorId = r.UserId, Name = r.ContactName, WST = r.WorkingStartTime, WET = r.WorkingEndTime, TimeZone = new TimeZoneModal { Name = t.Name, TimezoneId = t.TimezoneId, Offset = t.Offset, OffsetShort = t.OffsetShort } } ).ToList(); // Get avg jobs per hour or each operator foreach (var data in operators) { HSCodeAvgOutput jobOutput = new HSCodeAvgOutput(); var mycount = (from cnt in dbContext.eCommerceShipmentDetails join ec in dbContext.eCommerceShipments on cnt.eCommerceShipmentId equals ec.eCommerceShipmentId where !string.IsNullOrEmpty(cnt.HSCode) && ec.AssignedTo == data.OperatorId && ec.MappedOn != null group ec by ec.eCommerceShipmentId into g select new { name = g.Key, MappedOn = System.Data.Entity.DbFunctions.TruncateTime(g.FirstOrDefault().MappedOn.Value), count = g.Count() } ).ToList(); var count = mycount.GroupBy(x => x.MappedOn.Value.Date) .Select(AS => new { name = AS.Key, count = AS.Count() }).ToList().Count; var wst = UtilityRepository.GetFormattedTimeString(UtilityRepository.GetTimeZoneTime(data.WST, data.TimeZone.Name)); var wet = UtilityRepository.GetFormattedTimeString(UtilityRepository.GetTimeZoneTime(data.WET, data.TimeZone.Name)); DateTime dtFrom = DateTime.Parse(wst); DateTime dtTo = DateTime.Parse(wet); var timeDiffHrs = dtTo.Subtract(dtFrom).Hours; var timeDiffMins = dtTo.Subtract(dtFrom).Minutes; timeDiffHrs = timeDiffMins / 60 + timeDiffHrs; var avgjobs = mycount.Count / timeDiffHrs * count; jobOutput.AvgJobs = avgjobs; jobOutput.Name = data.Name; jobOutput.OperatorId = data.OperatorId; list.Add(jobOutput); } int sum = 0; foreach (var data in list) { sum += data.AvgJobs; } if (list.Count > 0) { sum = sum / list.Count; } else { sum = 0; } return(sum); } catch (Exception ex) { return(0); } }