dfmmalaw
5/15/2017 - 5:04 PM

Looping through list of items and removing them from the database

Looping through list of items and removing them from the database

public void removeViews(List<long> selectedClientViewIds, List<long> volPoolIds, long? clientId)
        {          
            if (selectedClientViewIds.Count == 0)
                return;

            foreach (var volPoolId in volPoolIds)
            {
                bool hasChanges = false;
                VolunteerPool volPool = volunteerPoolRepository.findById(volPoolId);

                var vpcvList = volunteerPoolClientViewRepository.findByVolunteerPoolId(volPoolId);

                foreach (var selectedClientViewId in selectedClientViewIds)
                {
                    VolunteerPoolClientView vpcvToRemove = vpcvList.FirstOrDefault(x => x.ClientViewId == selectedClientViewId);
                    if (vpcvToRemove != null)
                    {                        
                        db.VolunteerPoolClientViews.Remove(vpcvToRemove);
                        hasChanges = true;
                    }
                }
                if (hasChanges)
                {
                    db.SaveChanges();
                    volunteerPoolCalcService.updateCache(volPoolId);
                    if (volPool.CandidateId != null)
                        db.OSScoreCalc(null, volPool.CandidateId, null);
                }
            }            
        }