Search code examples
c#listentity-frameworktake

How can I get the following items from a list with entity framework?


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


Solution

  • 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