public async Task HandleAsync(EditDoDReportCommand request) { if (request.DodReports.Count < 1) { return; } var projectId = request.DodReports[0].ProjectId; var yearWeek = request.DodReports[0].YearWeek; var listMetricId = request.DodReports.Select(d => d.MetricId).Distinct(); var project = await _dbContext.Projects.FirstAsync(p => p.Id == projectId); var listMetric = await _dbContext.Metrics.Where(m => listMetricId.Contains(m.Id)) .ToListAsync(); request.DodReports.ForEach(d => { d.Project = project; d.Metric = listMetric.First(m => m.Id == d.MetricId); }); var dodReportsInDb = await _dbContext.DoDReports .AsNoTracking() .Where(r => r.ProjectId == projectId && r.YearWeek == yearWeek) .ToListAsync(); var dodReports = request.DodReports.Select(d => new DoDReport(d.ProjectId, d.Value, d.YearWeek, d.MetricId, d.ReportFileName, d.LinkToReport, d.Metric, d.Project)).ToList(); var recordsToAdd = dodReports.Except(dodReportsInDb, DoDReport.DoDComparer); var recordsToRemove = dodReportsInDb.Except(dodReports, DoDReport.DoDComparer); var recordsToUpdate = dodReports.Intersect(dodReportsInDb, DoDReport.DoDComparer); await _dbContext.DoDReports.AddRangeAsync(recordsToAdd); _dbContext.DoDReports.RemoveRange(recordsToRemove); _dbContext.UpdateRange(recordsToUpdate); await _dbContext.SaveChangesAsync(); request.Response = 1; }