How to use Skip and Take with Pagedlist Helper

Asked

Viewed 1,517 times

4

I would like to make the pagination, but using Skip and Take on my LINQ not to fetch all results.

Currently my code is 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));
    }

As use Skip and Take on my LINQ?

3 answers

4

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

var grupos= (from s in db.grupos
             select s).Skip(10).Take(20);
  • Yes, but where do these values 10, 20 come from using the Helper?

  • It’s any value I used as an example. Usually you calculate the values according to the current page. In Skip you can pass (page - 1) * size and in Take only the size; where page is the current page and size the amount of records presented.

4


As our friend Maria pointed out, you can use the Skip()and Take(), but the 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’s not necessary, since this is already done for you internally at ToPagedList().

2

Internally the PagedList, automatically makes the Skip and Take, and it only brings the data needed to generate the page. In its algorithm it calculates the amount of page and as per the parameter page brings the correct page you need to display!

Or you use it without it Skip and Take, or you use PagedList, i.e., the two at the same time have no purpose, either one (Skip and Take) or the other (Pagedlist).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.