1

I have one list which has more than 2k records. I want that each time first 100 records must get fetched and next time next 100 records must be fetched and so on . I am using take method of list but it is giving me first 100 only every time. Below is my query:

task = dataAccessObjects.GetDataToArchive();
foreach(var rec in task)
{
  var f = task.Take(100);
}
Rahul
  • 156
  • 2
  • 21

1 Answers1

7
int i =0;
foreach(var rec in task)
{
  var f = task.Skip(i*100).Take(100);
  i++;
}

Use Skip function to skip already selected records

Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
  • 1
    Readable but unfortunately not very efficient. You could use `Batch` from MoreLINQ. http://stackoverflow.com/a/11427448/284240 – Tim Schmelter May 18 '16 at 10:43
  • Thanks Akshey. Really appreciate your effort reply on my post. That worked. – Rahul May 18 '16 at 10:45
  • 1
    @DarshanPatel: just look at the link ;) – Tim Schmelter May 18 '16 at 10:46
  • @TimSchmelter : could you please paste alternative code for this? – Rahul May 18 '16 at 10:46
  • @Rahul you are welcome. happy to help – Akshey Bhat May 18 '16 at 10:46
  • @Rahul: this is a duplicate, but i have found another approach which i've posted some time ago which doesnt use Jon's `MoreLinq`(recommandable): [This `Batch`-extension method](http://stackoverflow.com/a/13183592/284240) is more efficient than the `Skip...Take`-approach. – Tim Schmelter May 18 '16 at 10:50
  • Or you could use [this method](http://stackoverflow.com/a/11775295/106159) which doesn't use any additional space (but which isn't threadsafe, if you care about that). But I'd say, whatever you do, don't use the very inefficient solution with Skip/Take above (use Tim's, Jon Skeet's or mine instead) – Matthew Watson May 18 '16 at 11:05