update

28810
2020-09-06 13:11:51 +08:00
parent ba5b0cff6f
commit efb239a1f5
3 changed files with 10 additions and 11 deletions

@@ -3,7 +3,6 @@ static 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")
.Build(); //请务必定义成 Singleton 单例模式
[Table(Name = "tb_topic")]
class Topic {
[Column(IsIdentity = true, IsPrimary = true)]
public int Id { get; set; }
@@ -16,15 +15,15 @@ class Topic {
## 分组聚合
```csharp
var groupby = fsql.Select<Topic>()
var list = 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
.ToList(a => new { a.Key, cou1 = a.Count(), arg1 = a.Avg(a.Value.Clicks) });
//SELECT substr(a.`Title`, 1, 2) as1, count(1) as2, avg(a.`Id`) as3
//FROM `Topic` 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

@@ -69,8 +69,8 @@ fsql.Select<Topic>().From<Category, CategoryType>((s, b, c) => s
```csharp
fsql.Select<Topic, Category, CategoryType>()
.WithSql(
"select * from Topic where id=@id1",
"select * from Category where id=@id2",
"select * from Topic where id=?id1",
"select * from Category where id=?id2",
null, //不设置 CategoryType 对应的 SQL
new { id1 = 10, id2 = 11, id3 = 13 }
)
@@ -78,8 +78,8 @@ fsql.Select<Topic, Category, CategoryType>()
.LeftJoin((a,b,c) => b.ParentId == c.Id)
.ToList();
//SELECT ...
//FROM ( select * from Topic where id=@id1 ) a
//LEFT JOIN ( select * from Category where id=@id2 ) b ON a.`CategoryId` = b.`Id`
//FROM ( select * from Topic where id=?id1 ) a
//LEFT JOIN ( select * from Category where id=?id2 ) b ON a.`CategoryId` = b.`Id`
//LEFT JOIN `CategoryType` c ON b.`ParentId` = c.`Id`
```

@@ -5,7 +5,7 @@ FreeSql 采用 ExpressionTree 优化读取速读,如果懂技术的你一定
Topic t1 = fsql.Select<Topic>().ToOne();
```
> FreeSql有一个约定ToOne 永远返回 null 或 有数据的实体对象ToList 永远返回非 null 的 List\<实体类型\>
> FreeSql约定ToOne/First 永远返回 null 或 有数据的实体对象ToList 永远返回非 null 的 List\<实体类型\>
## 2、返回 List
```csharp
@@ -23,7 +23,7 @@ List<Category> t3 = fsql.Select<Category>.Where(a => a.Name = "家电").AsTreeCt
## 4、返回 List + 导航属性的数据
```csharp
List<Topic> t4 = fsql.Select<Topic>().LeftJoin(a => a.Type.Id == a.TypeId).ToList();
//此时会返回普通字段 + 导航对象 Type 的数据
//此时会查询 Topic普通字段 + 导航对象Type 字段
```
## 5、指定字段返回