2

We have a DB table having a binary column. Each row stores the file content of a file. We want to avoid loading the file content (each can be 10-20Mb in size) into app server's memory unless when necessary.

In the following queries, will the file content be loaded into app server's memory? If yes, during which function call?

// Query 1
dataContext.DataFiles.OrderBy(w => w.atimestamp).ToList();


// Query 2
var filesizes = from DataFiles in dataContext.DataFiles
                select DataFiles.FileContent.Length;
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Lapson
  • 53
  • 2

1 Answers1

0

Linq methods which return IEnumerable<TSource> or IQueriable<TSource> are implemented to be deferred executed. Being differed executed, only when you call ToList()/ToArray() (or one of the linq extension methods that do not return a collection like Sum()/Max()/FirstOrDefault() etc.) will actually be executed and will retrieve the data

In your first query, the OrderBy() is not yet executed until you try to retrieve specific items or call the ToList(). However, even though this is deferred executed, this one will have to consume the entire collection the moment you requested the first item (whereas Select() for example will continue to be deferred for the entire collection - see this for some more details)

In your second query, after that line, nothing will yet to be executed.

You can read this MSDN blog about linq and deferred execution and this question from yesterday that also askes about the matter of when linq and deferred

Community
  • 1
  • 1
Gilad Green
  • 36,708
  • 7
  • 61
  • 95