通过IQueryable的用foreach迭代结果在内存不足异常 [英] Iterating through IQueryable with foreach results in an out of memory exception

查看:350
本文介绍了通过IQueryable的用foreach迭代结果在内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一个短小(10GB〜)表一个foreach / IQueryable的和LINQ到SQL迭代。
看起来是这样的:使用

I'm iterating through a smallish (~10GB) table with a foreach / IQueryable and LINQ-to-SQL. Looks something like this:

using (var conn = new DbEntities() { CommandTimeout = 600*100})
{
     var dtable = conn.DailyResults.Where(dr => dr.DailyTransactionTypeID == 1);
     foreach (var dailyResult in dtable)
     {
        //Math here, results stored in-memory, but this table is very small. 
        //At the very least compared to stuff I already have in memory. :)
     }
}



Visual Studio调试器抛出一个不折不扣的不一会在foreach循环的基础后,内存异常。我假设dtable的行不会被刷新。该怎么办?

The Visual Studio debugger throws an out-of memory exception after a short while at the base of the foreach loop. I'm assuming that the rows of dtable are not being flushed. What to do?

推荐答案

的IQueryable< DailyResult> dtable将试图加载整个foreach循环的迭代之前列举的时候...查询结果到内存中。它foreach循环的迭代过程中不加载一行。如果你想要的行为,使用 DataReader的

The IQueryable<DailyResult> dtable will attempt to load the entire query result into memory when enumerated... before any iterations of the foreach loop. It does not load one row during the iteration of the foreach loop. If you want that behavior, use DataReader.

这篇关于通过IQueryable的用foreach迭代结果在内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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