public async Task DeleteOpportunityItemAsync(int opportunityId, int opportunityItemId) { var referralItems = _referralRepository.GetManyAsync(referral => referral.OpportunityItemId == opportunityItemId); var provisionGaps = _provisionGapRepository.GetManyAsync(gap => gap.OpportunityItemId == opportunityItemId); await _referralRepository.DeleteManyAsync(referralItems.ToList()); await _provisionGapRepository.DeleteManyAsync(provisionGaps.ToList()); await _opportunityItemRepository.DeleteAsync(opportunityItemId); var opportunityItems = _opportunityItemRepository.GetManyAsync(item => item.OpportunityId == opportunityId); if (!opportunityItems.Any(item => item.IsSaved)) { var items = opportunityItems .Where(item => item.IsSaved == false) .ToList(); foreach (var opportunityItem in items) { await _referralRepository.DeleteManyAsync(_referralRepository.GetManyAsync(rf => rf.OpportunityItemId == opportunityItem.Id).ToList()); await _provisionGapRepository.DeleteManyAsync(_provisionGapRepository.GetManyAsync(gap => gap.OpportunityItemId == opportunityItem.Id).ToList()); await _opportunityItemRepository.DeleteAsync(opportunityItem); } await _opportunityRepository.DeleteAsync(opportunityId); } }