c# – 使用LINQ处理大型数据集
发布时间:2020-12-15 04:12:46 所属栏目:百科 来源:网络整理
导读:每次我使用LINQ to SQL编写下面表单的程序时,我最终会得到一个程序,它在运行时会抓取越来越多的内存,并且在可能只有25,000条记录之后会消耗掉2GB的堆.我总是最终使用ADO.NET重写它.我究竟做错了什么? 澄清:这个问题与加工速度无关;关于让它变得更快的答案
每次我使用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(); } } 解决方法
您正在对数据上下文施加压力,以便每次都从头开始生成查询.
请尝试使用已编译的查询. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |