每次我使用LINQ to SQL编写下面这种形式的程序时,都会得到一个随着运行而不断占用更多内存并在处理大约25,000条记录后崩溃的程序,最终我总是不得不使用ADO.NET重写它。我做错了什么?澄清:这个问题不是关于处理速度的,关于加速的答案与此无关。
foreach (int i=0; i<some_big_number; i++)
{
using (myDC dc = new myDC()) // my DataContext
{
myRecord record = (from r in dc.myTable where r.Code == i select r).Single();
// do some LINQ queries using various tables from the data context
// and the fields from this 'record'. i carefully avoid referencing
// any other data context than 'dc' in here because I want any cached
// records to get disposed of when 'dc' gets disposed at the end of
// each iteration.
record.someField = newValueJustCalculatedAbove;
dc.SubmitChanges();
}
}