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>();
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 分页;html
SqlServer 2012+ 版本,使用最新的 fetch next rows 分页;sql
返回前10条记录:select.Take(10).ToList();数据库
跳过前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 | 分页 |