How do I use Skip and Take with the PagedList Helper

4

I would like to paginate, but using Skip and Take in my LINQ not to fetch all results.

Currently my code looks like this:

    public ViewResult Index(int? page)
    {

        var grupos= from s in db.grupos
                     select s;

        int pageSize = 3;
        int pageNumber = (page ?? 1);
        return View(grupos.ToPagedList(pageNumber, pageSize));
    }

How do I use Skip and Take in my LINQ?

    
asked by anonymous 20.08.2014 / 19:23

3 answers

4

As our friend Maria pointed out, you can use Skip() and Take() , but PagedList already does it for you.

The replacement would be:

int pageSize = 3;
int pageNumber = (page ?? 1);

var grupos = db.grupos
                .Skip((pageSize - 1) * pageNumber)
                .Take(pageSize)
                .ToList();

return View(grupos);

But it is not necessary, since this is already done for you internally in ToPagedList() .

    
21.08.2014 / 15:50
4

There are no Take and Skip equivalents in the query syntax, but you can use the methods by doing a merge, as follows:

var grupos= (from s in db.grupos
             select s).Skip(10).Take(20);
    
20.08.2014 / 19:43
2

Internally the PagedList , automatically does the Skip and Take , and it only brings the necessary data to generate the page. In your algorithm it calculates the page quantity and as the paramemrix page brings the correct page you need to display!

Either you use it without it and it puts Skip and Take , or you use PagedList , that is, the two at the same time has no purpose, either one (Skip and Take) or another (PagedList ).

    
21.08.2014 / 15:21