views:

44

answers:

1

The following code retrieves all bytes data at once.

I'm wondering how to access byte data stored in chunk using entity framework. Since files are so big (around 50MB), I want to send it to user by chunks as soon as I get partial bytes from database.

using (Entities context = new Entities(EntitiesConnectionString))
{
    byte[] data = context.MyFileTable
        .Where(item => item.FileId == 1)
        .Select(item => FileData)
        .FirstOrDefault();
}

Thanks in advance!

+1  A: 

It is not possible. EF and LinQ in general enumerates from a source. It will only ever yield complete objects from that source.

I have 2 possible solutions:

  1. Query the data with your qualifying criteria that return a reference to what needs to be loaded, then stream in from there afterwards.

  2. Write a wrapper class that implements IEnumerable, let this return your data along with a stream object you can then interrogate. You can then query this object, and have the stream available to you on the result set.1.

Option #1 is probably the easiest.1.

Slappy
Thank you for the post. Do you know any web site that I can look up a sample code for either one of the solutions?
winmyan