FreeSql (十六)分页查询

IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
    .UseAutoSyncStructure(true) //自动同步实体结构到数据库
    .Build();

[Table(Name = "tb_topic")]
class Topic {
    [Column(IsIdentity = true, IsPrimary = true)]
    public int Id { get; set; }
    public int Clicks { get; set; }
    public int TestTypeInfoGuid { get; set; }
    public string Title { get; set; }
    public DateTime CreateTime { get; set; }
}

ISelect<Topic> select => fsql.Select<Topic>();

每页20条数据,查询第1页

var sql = select.Page(1, 20).ToSql();
///SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime` 
//FROM `tb_topic` a 
//LIMIT 0,20

优化

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

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

Take/Limit

返回前10条记录:select.Take(10).ToList();

Offset/Skip

跳过前10条记录,返回记录:select.Offset(10).ToList();

API

方法 返回值 参数 描述
ToSql string 返回即将执行的SQL语句
ToList List 执行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 分页

猜你喜欢

转载自www.cnblogs.com/FreeSql/p/11531341.html