自学内容网 自学内容网

EF Core分页

  1. Skip(3).Take(8) 最好显式指定排序规则
  2. 需要知道满足条件的数据的总条数:
    用IQueryable的复用
    LongCount和Count
  3. 页数:long pageCount = (long)Math.Ceiling(count * 1.0 / pageSize);
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);
        }
    }
}


原文地址:https://blog.csdn.net/Anoxia_li/article/details/145139634

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!