ancora update metodi DbFlush

This commit is contained in:
Samuele Locatelli
2026-04-08 13:13:40 +02:00
parent d6d7b9ff61
commit 7f825f2a09
9 changed files with 44 additions and 125 deletions
@@ -81,44 +81,9 @@ namespace MP.Data.Repository.Utils
}
}
// ora preparo inserimento massivo
var existingRecords = await dbCtx
.DbSetStatsAggr
.Where(x => listRecords.Select(r => r.Hour).Contains(x.Hour) &&
listRecords.Select(r => r.Destination).Contains(x.Destination))
.ToListAsync();
if (existingRecords.Count > 0)
{
var existingHours = existingRecords.Select(r => new { r.Hour, r.Destination }).ToHashSet();
var recordsToUpdate = listRecords.Where(r =>
existingHours.Contains(new { Hour = r.Hour, Destination = r.Destination })).ToList();
var recordsToAdd = listRecords.Where(r =>
!existingHours.Contains(new { Hour = r.Hour, Destination = r.Destination })).ToList();
foreach (var updateRec in recordsToUpdate)
{
var existing = existingRecords.First(x => x.Hour == updateRec.Hour && x.Destination == updateRec.Destination);
existing.RequestCount = updateRec.RequestCount;
existing.AvgDuration = updateRec.AvgDuration;
existing.MaxDuration = updateRec.MaxDuration;
existing.MinDuration = updateRec.MinDuration;
existing.NoReply = updateRec.NoReply;
}
if (recordsToAdd.Count > 0)
{
await dbCtx.DbSetStatsAggr.AddRangeAsync(recordsToAdd);
}
}
else
{
await dbCtx
await dbCtx
.DbSetStatsAggr
.AddRangeAsync(listRecords);
}
// salvo!
answ = await dbCtx.SaveChangesAsync();