Basically I use Entity Framework to query a huge database. I want to return a string list then log it to a text file.
List<string> logFilePathFileName = new List<string>();
var query = from c in DBContext.MyTable where condition = something select c;
foreach (var result in query)
{
filePath = result.FilePath;
fileName = result.FileName;
string temp = filePath + "." + fileName;
logFilePathFileName.Add(temp);
if(logFilePathFileName.Count %1000 ==0)
Console.WriteLine(temp+"."+logFilePathFileName.Count);
}
However I got an exception when logFilePathFileName.Count=397000.
The exception is:
Exception of type ‘System.OutOfMemoryException’ was thrown.
A first chance exception of type ‘System.OutOfMemoryException’
occurred in System.Data.Entity.dll
UPDATE:
What I want to use a different query say: select top 1000 then add to the list, but I don’t know after 1000 then what?
Most probabbly it’s not about a
RAMas is, so increasing yourRAMor even compiling and running your code in64bit machine will not have a positive effect, in this case.I think it’s related to a fact that
.NETcollections are limited to maximum2GBRAM space (no difference either32or64bit).To resolve this, split your list to much smaller chunks and most probabbly your problem will gone.
Just one possible solution:
EDIT
If you want fragment a query, you can use
Skip(...)andTake(...)Just an explanatory example:
…
and so on..
Naturally put it in your iteration and parametrize it based on bounds of data you know or need.