C#/Entity Framework Core Using Linq for Pagination: How to Use .Skip() and .Take()

2019年12月9日 62点热度 0人点赞 0条评论
内容目录

The general usage format is

int pagesize  = page size (size of each page)
int pageindex = the page number (this variable generally increments with each loop)
Usage
.Skip(pagesize*pageindex).Take(pagesize)

.Skip()     ignores numbers, indicating where to start pagination

.Take()   indicates how many records to capture on each page

Note that the sequence starts from 0, meaning the first one is 0, the second is 1 ... ...

The above method combined captures only one page and needs to be used in a loop to continuously capture the next page

For example

{1,2,3,4,5,6,7,8,9,10}
.Skip(5).Take(4) // ignore 5 numbers, starting from the (5+1)th number!A total of 4 captured
Output: 6,7,8,9

Let's take a look at another example

  

            List<string> Name = new List<string> {
                "Zhang San1","Li Si2","Wang Chao3","Ma Han4","Zhang Long5", "Zhao Hu6","Wang Xi7","Who is 8","Lu Xiao Yu9", "Haha10","Kill Count11"
            };
            var item1 = Name.Skip(5).Take(4);
            foreach (var i in item1)
            {
                Console.WriteLine(i);
            }
Output
Zhao Hu6
Wang Xi7
Who is 8
Lu Xiao Yu9

 

Skip(5) does not mean to start capturing from the 5th, but to ignore the previous 5

Got it?

However, the previous example only captures a segment of the content and does not count as pagination. Here’s a simple example of pagination.

Writing in a C# console (to make it easier to understand, please copy and run it in your VS)

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1 { class Program { static void Main(string[] args) { List<string> Name = new List<string> { //Data "Zhang San1","Li Si2","Wang Chao3","Ma Han4","Zhang Long5", "Zhao Hu6","Wang Xi7","Who is 8","Lu Xiao Yu9", "Hero10","Superman11" }; int pagesize = 4; //Number of items per page for (int i = 0; i * pagesize < Name.Count; i++) //i is defaulted to 0, using i to denote page number { var items = Name.Skip(i * pagesize).Take(pagesize); //Capture the i-th page Console.WriteLine($"Page {i + 1}");
foreach (var item in items) //Output the content of each page { Console.WriteLine(" " + item);
} } Console.ReadKey(); }

}

}

 

Output
Page 1
    Zhang San1
    Li Si2
    Wang Chao3
    Ma Han4
Page 2
    Zhang Long5
    Zhao Hu6
    Wang Xi7
    Who is 8
Page 3
    Lu Xiao Yu9
    Haha10
    Kill Count11

So, how to apply it in ASP.NET Core/Entity Framework Core?

Assuming there is a news list page as follows

My idea is,

Capture only one page at a time. When the user clicks a specific page, the list for that page will be captured.

This simplifies the complexity of loops and algorithm implementation.

Example

        /// <summary>
        /// Get the news list for pageIndex page
        /// </summary>
        /// <param name="pageSize">Size of each page</param>
        /// <param name="pageIndex">Which page</param>
        public IEnumerable<News> Test(int pageSize,int pageIndex)
        {
            var NewsLists = _db.News.ToList();  //Get data from the News table in the database
            var items = NewsLists.Skip(pageSize * (pageIndex - 1)).Take(pageSize);   //List for which page
            return items;
        }

Of course, the example above simply captures one page and returns an object. No data processing is performed.

If you want to retrieve data all at once and create a paginated list, that can be quite troublesome.

Assuming the data is one-dimensional, the paginated data is equivalent to two-dimensional.

Sometimes it is not necessary to view all the lists at once, especially when the data is to be divided into thousands, and users only look at one page~~~~~.

------------

I am a beginner and have limited skills, so please feel free to criticize.

痴者工良

高级程序员劝退师

文章评论