Modifica upsert dati
This commit is contained in:
@@ -82,9 +82,43 @@ namespace MP.Data.Repository.Utils
|
||||
}
|
||||
|
||||
// ora preparo inserimento massivo
|
||||
await dbCtx
|
||||
var existingRecords = await dbCtx
|
||||
.DbSetStatsAggr
|
||||
.AddRangeAsync(listRecords);
|
||||
.Where(x => listRecords.Select(r => r.Hour).Contains(x.Hour) &&
|
||||
listRecords.Select(r => r.Type).Contains(x.Type))
|
||||
.ToListAsync();
|
||||
|
||||
if (existingRecords.Count > 0)
|
||||
{
|
||||
var existingHours = existingRecords.Select(r => new { r.Hour, r.Type }).ToHashSet();
|
||||
|
||||
var recordsToUpdate = listRecords.Where(r =>
|
||||
existingHours.Contains(new { Hour = r.Hour, Type = r.Type })).ToList();
|
||||
|
||||
var recordsToAdd = listRecords.Where(r =>
|
||||
!existingHours.Contains(new { Hour = r.Hour, Type = r.Type })).ToList();
|
||||
|
||||
foreach (var updateRec in recordsToUpdate)
|
||||
{
|
||||
var existing = existingRecords.First(x => x.Hour == updateRec.Hour && x.Type == updateRec.Type);
|
||||
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
|
||||
.DbSetStatsAggr
|
||||
.AddRangeAsync(listRecords);
|
||||
}
|
||||
|
||||
// salvo!
|
||||
answ = await dbCtx.SaveChangesAsync();
|
||||
|
||||
Reference in New Issue
Block a user