使用LINQ处理大型数据集 [英] Processing large datasets using LINQ

查看:90
本文介绍了使用LINQ处理大型数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我使用LINQ to SQL编写下面形式的程序时,最终都会得到一个程序,该程序在运行时会捕获越来越多的内存,并且可能在经过少至25,000条记录后就消耗了2GB的堆空间.我总是最终使用ADO.NET重写它.我在做什么错了?

Every time I write a program of the form below using LINQ to SQL, I end up with a program that just grabs more and more memory as it runs and falls over in a heap consuming 2GB after perhaps as little as 25,000 records. I always end up rewriting it using ADO.NET. What am I doing wrong?

说明:此问题与处理速度无关;使它运行得更快的答案没有任何意义.

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();
    }
}

推荐答案

您正向数据上下文施加压力,每次都要从头开始生成查询.

You are putting pressure on the data context to generate the query from scratch every time.

尝试改用编译查询.

这篇关于使用LINQ处理大型数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆