I have 2 tables both with relatively large amounts of information. As part of the process I need to update Table 2(Inv) with the information from Table 1. Essentially copy the CC value from Table 1(Acc) to PCC value in Table 2.
The amount of updates vary from 4000 to 40000 rows and the page hangs for quite a while. I cannot run this in the background because another process has to run directly after it which needs this update to happen first and output to user.
How can I speed things up?
Current code:
var invList = db.Inv.Where(m => m.ID == GI.Id).ToList();
var invListAcc = invList .Select(m => m.AccNum).ToList();
var accExtract = db.Acc.Where(m => invListAcc .Contains(m.AccNum)).ToList();
foreach(var inv in invList)
{
var invDB = db.Inv.Find(inv.Id);
var accCC = accExtract.Where(m => m.AccNum== invDB.AccNum).Select(p=>p.CC).FirstOrDefault();
if(accCC != null)
{
invDB.PCC = accCC;
db.Entry(invDB).State = EntityState.Modified;
db.SaveChanges();
}
}
Table : Acc
+------------+---------+
| AccNum | CC |
+------------+---------+
| 123 | 34 |
| 124 | 24 |
+------------+---------+
Table : Inv
+-------+------------+---------+
| Id | AccNum | PCC |
+-------+------------+---------+
| 1 | 123 | |
| 2 | 124 | |
+-------+------------+---------+