IFreeSql fsql =2904628156 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")
.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>();
Packet aggregation
var groupby = fsql.Select<Topic>()
.GroupBy(a => new { tt2 = a.Title.Substring(0, 2), mod4 = a.Id % 4 }) .Having(a => a.Count() > 0 && a.Avg(a.Key.mod4) > 0 && a.Max(a.Key.mod4) > 0) .Having(a => a.Count() < 300 || a.Avg(a.Key.mod4) < 100) .OrderBy(a => a.Key.tt2) .OrderByDescending(a => a.Count()) .ToList(a => new { a.Key.tt2, cou1 = a.Count(), arg1 = a.Avg(a.Key.mod4) }); //SELECT substr(a.`Title`, 1, 2) as1, count(1) as2, avg((a.`Id` % 4)) as3 //FROM `xxx` a //GROUP BY substr(a.`Title`, 1, 2), (a.`Id` % 4) //HAVING (count(1) > 0 AND avg((a.`Id` % 4)) > 0 AND max((a.`Id` % 4)) > 0) AND (count(1) < 300 OR avg((a.`Id` % 4)) < 100) //ORDER BY substr(a.`Title`, 1, 2), count(1) DESC
API
method | return value | parameter | description |
---|---|---|---|
ToSql | string | Returns the SQL statement to be executed | |
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 |
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 |
[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}) |
【member】 | |||
Key | Returns the object selected GroupBy | ||
Value | Back to the main table or From <T2, T3 ....> field selector |
Series navigation
-
(Xxiii) a packet, the polymerization
-
(Xxvii) that has been written SQL statement, and the second entity class mapping query
IFreeSql fsql =2904628156 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")
.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>();
Packet aggregation
var groupby = fsql.Select<Topic>()
.GroupBy(a => new { tt2 = a.Title.Substring(0, 2), mod4 = a.Id % 4 }) .Having(a => a.Count() > 0 && a.Avg(a.Key.mod4) > 0 && a.Max(a.Key.mod4) > 0) .Having(a => a.Count() < 300 || a.Avg(a.Key.mod4) < 100) .OrderBy(a => a.Key.tt2) .OrderByDescending(a => a.Count()) .ToList(a => new { a.Key.tt2, cou1 = a.Count(), arg1 = a.Avg(a.Key.mod4) }); //SELECT substr(a.`Title`, 1, 2) as1, count(1) as2, avg((a.`Id` % 4)) as3 //FROM `xxx` a //GROUP BY substr(a.`Title`, 1, 2), (a.`Id` % 4) //HAVING (count(1) > 0 AND avg((a.`Id` % 4)) > 0 AND max((a.`Id` % 4)) > 0) AND (count(1) < 300 OR avg((a.`Id` % 4)) < 100) //ORDER BY substr(a.`Title`, 1, 2), count(1) DESC
API
method | return value | parameter | description |
---|---|---|---|
ToSql | string | Returns the SQL statement to be executed | |
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 |
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 |
[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}) |
【member】 | |||
Key | Returns the object selected GroupBy | ||
Value | Back to the main table or From <T2, T3 ....> field selector |
Series navigation
-
(二十三)分组、聚合