0

I have a very large list and I want to extract in parts of 50 items for which I will implement the following:

try
{
    using (var context = new ccoFinalEntities())
    {
        return context.sales
            .Where(p => true == p.status && myID == p.id)
            .Take(50)
            .ToList();
    }
}
catch
{
    return null;
}

I assume this will return me the first 50 items, my question is:

How can I get the next 50 and so on until extract the list completely?

Any comments or suggestions are welcome

mihai.luca
  • 67
  • 1
  • 6
Fabián Romo
  • 319
  • 2
  • 14
  • FIrst time you call `return`, your method will exit and you will need to cal the method again. – Matt Aug 23 '19 at 12:46
  • Use Select(x,i) => where 'i' is the index. Then use GroupBy i/50 to get groups of 50. – jdweng Aug 23 '19 at 12:50

1 Answers1

1
public List<Item> GetDate(int page, int size = 50)
    {
    try
    {
      using (var context = new ccoFinalEntities())
      {
        return context.sales.Where(p => true == p.status && myID == p.id)
                            .Skip(page * size).Take(size).ToList();
      }
    }
    catch
    {
      return null;
    }
}

Then call:

GetData(0);
GetData(1);
GetData(2);
GetData(3);

.....

Skip ignore first page * size (1 * 50, 2 * 50, ...) items and Take size (50) item

Antoine V
  • 6,998
  • 2
  • 11
  • 34