/// <summary> /// Deletes the items from the database. /// </summary> /// <param name="serviceProvider">The application service provider.</param> /// <param name="token">The cancellation token for the task.</param> public async Task DeleteAsync(IServiceProvider serviceProvider, CancellationToken token) { // Check if there weren't any valid items found. if (Items == null) { // Throw an exception. throw new TaskException("No valid items could be found with the provided data."); } // Get the total number of batches. var count = Math.Ceiling((double)Items.Count() / ApplicationDbContext.BatchSize); // Go over each batch. for (var index = 0; index < count; index++) { // Check if the cancellation was requested. if (token.IsCancellationRequested) { // Break. break; } // Get the items in the current batch. var batchItems = Items .Skip(index * ApplicationDbContext.BatchSize) .Take(ApplicationDbContext.BatchSize); // Get the IDs of the items in the current batch. var batchIds = batchItems.Select(item => item.Id); // Define the list of items to get. var paths = new List <Path>(); // Use a new scope. using (var scope = serviceProvider.CreateScope()) { // Use a new context instance. using var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); // Get the items with the provided IDs. var items = context.Paths .Where(item => batchIds.Contains(item.Id)); // Check if there were no items found. if (items == null || !items.Any()) { // Continue. continue; } // Get the items found. paths = items .ToList(); } // Get the IDs of the items. var pathIds = paths .Select(item => item.Id); // Delete the related entities. await PathExtensions.DeleteRelatedEntitiesAsync <PathNode>(pathIds, serviceProvider, token); await PathExtensions.DeleteRelatedEntitiesAsync <PathEdge>(pathIds, serviceProvider, token); // Delete the items. await IEnumerableExtensions.DeleteAsync(paths, serviceProvider, token); } }
/// <summary> /// Deletes the items from the database. /// </summary> /// <param name="serviceProvider">The application service provider.</param> /// <param name="token">The cancellation token for the task.</param> public async Task DeleteAsync(IServiceProvider serviceProvider, CancellationToken token) { // Check if there weren't any valid items found. if (Items == null) { // Throw an exception. throw new TaskException("No valid items could be found with the provided data."); } // Get the total number of batches. var count = Math.Ceiling((double)Items.Count() / ApplicationDbContext.BatchSize); // Go over each batch. for (var index = 0; index < count; index++) { // Check if the cancellation was requested. if (token.IsCancellationRequested) { // Break. break; } // Get the items in the current batch. var batchItems = Items .Skip(index * ApplicationDbContext.BatchSize) .Take(ApplicationDbContext.BatchSize); // Get the IDs of the items in the current batch. var batchIds = batchItems .Where(item => item.Analysis != null && !string.IsNullOrEmpty(item.Analysis.Id)) .Where(item => item.User != null && !string.IsNullOrEmpty(item.User.Id)) .Select(item => (item.Analysis.Id, item.User.Id)); // Get the IDs of all individual items. var batchAnalysisIds = batchIds .Select(item => item.Item1); var batchUserIds = batchIds .Select(item => item.Item2); // Define the list of items to get. var analysisUsers = new List <AnalysisUser>(); // Create a new scope. using (var scope = serviceProvider.CreateScope()) { // Use a new context instance. using var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); // Get the items with the provided IDs. var items = context.AnalysisUsers .Include(item => item.Analysis) .Include(item => item.User) .Where(item => batchAnalysisIds.Contains(item.Analysis.Id)) .Where(item => batchUserIds.Contains(item.User.Id)) .AsEnumerable() .Where(item => batchIds.Any(item1 => item1.Item1 == item.Analysis.Id && item1.Item2 == item.User.Id)) .ToList(); // Check if there were no items found. if (items == null || !items.Any()) { // Continue. continue; } // Get the items found. analysisUsers = items .ToList(); } // Delete the items. await IEnumerableExtensions.DeleteAsync(analysisUsers, serviceProvider, token); } }