public async Task RecalculateShiftWorkItemAssignments(List <ScheduledShiftAssignmentDto> scheduledShiftAssignments) { var scheduledShiftsForRecalc = scheduledShiftAssignments .GroupBy(x => x.Id) .Select(x => x.First()) .GroupBy(x => new { x.ScheduledShiftId, x.StartsAt, x.EndsAt }) .OrderBy(x => x.Key.StartsAt); var scheduledShiftIds = scheduledShiftsForRecalc.Select(x => x.Key.ScheduledShiftId).Distinct().ToList(); var allWorkItems = (await _workItemRepository.GetWorkItemsForShifts(scheduledShiftIds)).ToList(); var allWorkItemAssignments = (await _workItemRepository.GetWorkItemAssignmentsForShifts(scheduledShiftIds)).ToList(); var allRecurringWorkItems = (await _workItemRepository.GetRecurringWorkItemsForShifts(scheduledShiftIds)).ToList(); var allShiftRecurringWorkItems = (await _workItemRepository.GetShiftRecurringWorkItemsForShifts(scheduledShiftIds)).ToList(); foreach (var scheduledShift in scheduledShiftsForRecalc) { await RecalculateSingleShiftWorkItemAssignments(scheduledShift.Key.ScheduledShiftId, scheduledShift.ToList(), allWorkItems, allWorkItemAssignments, allRecurringWorkItems, allShiftRecurringWorkItems); } }