分页查询
2881099 edited this page 2022-08-06 00:25:19 +08:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

中文 | English

static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.MySql, connectionString)
    .Build(); //请务必定义成 Singleton 单例模式

class Topic {
    [Column(IsIdentity = true)]
    public int Id { get; set; }
    public string Title { get; set; }
    public int Clicks { get; set; }
    public DateTime CreateTime { get; set; }

    public int CategoryId { get; set; }
}

每页20条数据查询第1页

var list = fsql.Select<Topic>()
    .Where(a => a.Id > 10)
    .Count(out var total) //总记录数量
    .Page(1, 20)
    .Tolist();

Count(out var total) 是同步方法,原因是 out 不支持异步,如果介意可以单独执行如下:

提示:数据量大一般不建议查 Count/CountAsync而应该采用流式分页上一页、下一页、不返回总数量

var select = fsql.Select<Topic>()
    .Where(a => a.Id > 10);
var total = await select.CountAsync();
var list = await select.Page(1, 20).ToListAsync();

优化

SqlServer 2012 以前的版本,使用 row_number 分页;

SqlServer 2012+ 版本,使用最新的 fetch next rows 分页;

API

方法 返回值 参数 描述
ToSql string 返回即将执行的SQL语句
ToList List<T1> 执行SQL查询返回 T1 实体所有字段的记录,若存在导航属性则一起查询返回,记录不存在时返回 Count 为 0 的列表
ToList<T> List<T> Lambda 执行SQL查询返回指定字段的记录记录不存在时返回 Count 为 0 的列表
ToList<T> List<T> string field 执行SQL查询返回 field 指定字段的记录,并以元组或基础类型(int,string,long)接收,记录不存在时返回 Count 为 0 的列表
【分页】
Count long 查询的记录数量
Count <this> out long 查询的记录数量以参数out形式返回
Skip <this> int offset 查询向后偏移行数
Offset <this> int offset 查询向后偏移行数
Limit <this> int limit 查询多少条数据
Take <this> int limit 查询多少条数据
Page <this> int pageIndex, int pageSize 分页

参考资料