- Skip(3).Take(8) 最好显式指定排序规则
- 需要知道满足条件的数据的总条数:
用IQueryable的复用
LongCount和Count - 页数:long pageCount = (long)Math.Ceiling(count * 1.0 / pageSize);
cs
class Program
{
static async Task Main(string[] args)
{
using (MyDbContext ctx = new MyDbContext())
{
PrintPage(1, 1);
}
}
/// <summary>
/// 打印某页数据
/// </summary>
/// <param name="pageIndex">页码(从1开始)</param>
/// <param name="pageSize">每页数据条数</param>
static void PrintPage(int pageIndex, int pageSize)
{
using (MyDbContext ctx = new MyDbContext())
{
IQueryable<Book> books = ctx.Books.Where(o => !o.Name.Contains("计算机"));
var item = books.Skip((pageIndex - 1) * pageSize).Take(pageSize);
foreach (Book book in item)
{
Console.WriteLine(book.Name);
}
long count = books.LongCount();
long pageCount = (long)Math.Ceiling((double)count / pageSize);
Console.WriteLine("总页数:" + pageCount);
}
}
}