ancora update metodi DbFlush
This commit is contained in:
@@ -102,43 +102,9 @@ namespace MP.Data.Repository.Utils
|
||||
}
|
||||
}
|
||||
|
||||
// ora preparo inserimento massivo
|
||||
var existingRecords = await dbCtx
|
||||
.DbSetStatsDet
|
||||
.Where(x => listRecords.Select(r => new { r.Hour, r.Destination, r.Type }).Contains(new { Hour = x.Hour, Destination = x.Destination, Type = x.Type }))
|
||||
.ToListAsync();
|
||||
|
||||
if (existingRecords.Count > 0)
|
||||
{
|
||||
var existingKeys = existingRecords.Select(r => new { r.Hour, r.Destination, r.Type }).ToHashSet();
|
||||
|
||||
var recordsToUpdate = listRecords.Where(r =>
|
||||
existingKeys.Contains(new { Hour = r.Hour, Destination = r.Destination, Type = r.Type })).ToList();
|
||||
|
||||
var recordsToAdd = listRecords.Where(r =>
|
||||
!existingKeys.Contains(new { Hour = r.Hour, Destination = r.Destination, Type = r.Type })).ToList();
|
||||
|
||||
foreach (var updateRec in recordsToUpdate)
|
||||
{
|
||||
var existing = Enumerable.First<StatsDetailModel>((IEnumerable<StatsDetailModel>)existingRecords, (Func<StatsDetailModel, bool>)(x => x.Hour == updateRec.Hour && x.Destination == updateRec.Destination && 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.DbSetStatsDet.AddRangeAsync(recordsToAdd);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await dbCtx
|
||||
await dbCtx
|
||||
.DbSetStatsDet
|
||||
.AddRangeAsync(listRecords);
|
||||
}
|
||||
|
||||
// salvo!
|
||||
answ = await dbCtx.SaveChangesAsync();
|
||||
|
||||
Reference in New Issue
Block a user