Nes*_*tor 6 c# linq linq-to-sql c#-3.0
每次我使用LINQ to SQL编写下面表单的程序时,我最终会得到一个程序,它在运行时会抓取越来越多的内存,并且在可能只有25,000条记录之后会消耗掉2GB的堆.我总是最终使用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();
}
}
Run Code Online (Sandbox Code Playgroud)