locked
looping through blocks of results RRS feed

  • Question

  • User-1766430311 posted

    Hiya.

    I'm a bit confused on how to go about the following:

    I have a query that returns 500,000 results. With the results, I need to modify files, so this becomes very time consuming when saving back to the DB. I'd like to query the 500k results, then work on 1000 results at a time. My query is as follows:

    var q = (from i in ctx.Images
                             where i.Hash == null
                             select i).Take(1000);


    I am manually taking 1000 as i'm not sure how to grab the 500,000 and work my way through while saving after each 1000.

    Any help would be appreciated.

    Edit: just to clarify - I know how to loop through it, but not without re-querying again. I'd like to use the existing query if possible.

    Monday, June 15, 2015 5:16 PM

Answers

  • User2024324573 posted

    You can try something like this:

    var q = ctx.Images.Where(i=>i.Hash==null).ToList();
    //for the first iteration
    var first1000 = q.Take(1000)
    //then you can iterate through
    for(int i=0; i<1000, i++)
    {
    var nextimage1000 = q.Skip(1000).Take(1000);
    }

    Hope this will help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 15, 2015 5:45 PM