I have a table with half a million rows. I need to update every single row but the ToList() fails:
List allContacts = objDatabase.Contacts.ToList
Here is a solution using chunking. It will dispose of the container (and the downloaded entities) after every chunk. Memory should be released by the GC long before your system runs out of memory.
int chunkSize = 50;
int curCount = 0;
while (true)
{
using (var db = new DbEntities())
{
var chunk = db.Contacts.Skip(curCount).Take(chunkSize).ToArray();
curCount += chunkSize;
if (chunk.Length == 0) break;
foreach (var contact in chunk)
{
//do any work for the contact here
contact.Something = "SomethingNew";
}
db.SaveChanges();
}
}
Feel free to play around with the chunk size. The larger the chunk, the faster the entire process should be, but it will use up more memory.