FreeSql (xv) query data

FreeSql foot in the data query function, the chain query syntax, multi-table query expression functions support the very place.

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>();

Query data

var sql = select.Where(a => a.Id == 10).ToSql();
///SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime` 
//FROM `tb_topic` a 
//WHERE (a.`Id` = 10)

sql = select.Where(a => a.Id == 10 && a.Id > 10 || a.Clicks > 100).ToSql();
///SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime` 
//FROM `tb_topic` a 
//WHERE (a.`Id` = 10 AND a.`Id` > 10 OR a.`Clicks` > 100)

sql = select.Where(a => new []{1,2,3}.Contains(a.Id)).ToSql();
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime` 
//FROM `tb_topic` a 
//WHERE (a.`Id` in (1,2,3))

API

method return value parameter description
ToSql string Returns the SQL statement to be executed
ToList List Execute SQL query and returns all fields of record T1 entity, if the navigation attribute exists with query returns Count returns, there is no record of when the list 0
ToList<T> List<T> Lambda Execute SQL query and returns the specified record fields, Count returned when no records exist to list 0
ToList<T> List<T> string field Executing the SQL query, returns the specified field recording field, and tuple or base type (int, string, long) receiving, recording absence Count returned list 0
ToOne T1 Executing the SQL query, returns the first record all the fields T1 entity, or null when there is no record
Any bool Execute SQL queries, whether there is record
Sum T Lambda Specify a column summation
me T Lambda Specify a column for the minimum
Max T Lambda Specify a column selecting the maximum value
Avg T Lambda Specify a column averaging
[Page]
Count long Record number of queries
Count <this> out long The number of records the query returns a parameter out form
Skip <this> int offset Queries offset rows back
Offset <this> int offset Queries offset rows back
Limit <this> int limit How many data query
Take <this> int limit How many data query
Page <this> int pageIndex, int pageSize Paging
【condition】
Where <this> Lambda It supports multi-table query expression
WhereIf <this> bool, Lambda It supports multi-table query expression
Where <this> string, parms Sql syntax native conditions, Where ( "id =? Id", new {id = 1})
WhereIf <this> bool, string, parms Sql syntax native conditions, WhereIf (true, "id =? Id", new {id = 1})
WhereCascade <this> Lambda When multi-table query, the conditions attached to each table
[Packet]
GroupBy <this> Lambda Grouped by columns selected, GroupBy (a => a.Name)
GroupBy <this> string, parms Sql original raw packet syntax, GroupBy ( "concat (name,? Cc)", new {cc = 1})
Having <this> string, parms Sql syntax polymerization conditions original green filter, Having ( "count (name) =? Cc", new {cc = 1})
Disdinct <this> .Distinct (). ToList (x => x.GroupName) is specified field
[Sort]
OrderBy <this> Lambda Sort by columns, OrderBy (a => a.Time)
OrderByDescending <this> Lambda Backward sorted by column, OrderByDescending (a => a.Time)
OrderBy <this> string, parms Original raw sql syntax sorting, OrderBy ( "count (name) +? Cc", new {cc = 1})
[Table] United
LeftJoin <this> Lambda Leftist query, use the navigation attributes, or associated entity type is specified
InnerJoin <this> Lambda Join query, navigation attributes may be used, or specify the type of the associated entity
RightJoin <this> Lambda Right associated query, navigation attributes may be used, or specify the type of the associated entity
LeftJoin <this> string, parms Leftist query, use native sql grammar, LeftJoin ( "type b on b.id = a.id and b.clicks>? Clicks", new {clicks = 1})
InnerJoin <this> string, parms Join query, using native sql syntax, InnerJoin ( "type b on b.id = a.id and b.clicks>? Clicks", new {clicks = 1})
RightJoin <this> string, parms Right-linked query, use native sql grammar, RightJoin ( "type b on b.id = a.id and b.clicks>? Clicks", new {clicks = 1})
From <this> Lambda Multi-table query, three tables are very convenient to use than the current maximum support 10 table design
【other】
As <this> string alias = "a" 指定别名
Master <this> 指定从主库查询(默认查询从库)
WithTransaction <this> DbTransaction 设置事务对象
WithConnection <this> DbConnection 设置连接对象

Guess you like

Origin www.cnblogs.com/FreeSql/p/11531339.html