private void AttemptToUnallocateCastings(IEnumerable <ScheduleWithQueues> schedules) { // If a schedule is decremented, there could be more allocated castings than total planned quantity. In that case, unallocate those // so that they can be assigned to a new schedule foreach (var job in schedules.GroupBy(s => s.Unique)) { var uniqueStr = job.Key; var proc1PathsByQueue = job .Where(sch => !string.IsNullOrEmpty(sch.Procs[1].InputQueue)) .GroupBy(p => p.Procs[1].InputQueue); foreach (var queueGroup in proc1PathsByQueue) { var matInQueue = _log.GetMaterialInQueue(queueGroup.Key) .Where(m => m.Unique == uniqueStr && FindNextProcess(m.MaterialID) == 1) .ToList() ; var extra = matInQueue.Count(); foreach (var sch in queueGroup) { extra -= sch.SchRow.PlanQuantity - CountCompletedOrMachiningStarted(sch); } if (extra > 0) { // no TakeLast in .NET framework _log.MarkCastingsAsUnallocated(matInQueue.Skip(Math.Max(0, matInQueue.Count() - extra)).Select(m => m.MaterialID)); } } } }