mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-07 00:40:55 +08:00
update
124
Return-Data.md
124
Return-Data.md
@@ -86,6 +86,68 @@ List<匿名类> t11 = fsql.Select<Topic>().ToList(a => new {
|
||||
|
||||
Reference to: https://github.com/dotnetcore/FreeSql/issues/528
|
||||
|
||||
## Dto Mapping Returning
|
||||
|
||||
```csharp
|
||||
fsql.Select<Song>().ToList<Dto>();
|
||||
//Case 1: The field with the same property name of Dto and Song is queried, and List<Dto> is returned
|
||||
|
||||
fsql.Select<Song>().ToList(a => new Dto { xxx = a.ext })
|
||||
//Case 2: The field with the same property name of Dto and Song is queried, the mapping ext is adjusted, and List<Dto> is returned
|
||||
|
||||
fsql.Select<Song>().ToList(a => new Song { id = a.id })
|
||||
//Case 3: The type specified by Lambda is the same as the type of Song, only the specified field id is queried, and List<Song> is returned
|
||||
|
||||
fsql.Select<Song>().ToList(a => new { id = a.id })
|
||||
//Case 4: Lambda specifies an anonymous type, only queries the specified field id, and returns List<ANONYMOUS_OBJECT>
|
||||
```
|
||||
|
||||
> Please handle the difference carefully.
|
||||
|
||||
```csharp
|
||||
fsql.Select<Song>().ToList(a => new Dto(a.id))
|
||||
//Case 5: Only query id and return List<Dto>
|
||||
|
||||
fsql.Select<Song>().ToList(a => new Dto(a.id) { xxx = a.ext })
|
||||
//Case 6: Query id, ext and return List<Dto>
|
||||
|
||||
fsql.Select<Song>().ToList(a => new Song(a.id))
|
||||
//Case 7: Query id and return List<Song>
|
||||
|
||||
fsql.Select<Song>().ToList(a => new Song(a.id) { xxx = a.ext })
|
||||
//Case 8: Query id, ext and return List<Song>
|
||||
```
|
||||
|
||||
> All methods of GroupBy are not applicable to DTO mapping rules
|
||||
|
||||
This kind of mapping supports single table/multi-table, mapping before querying data (not to query all fields first and then to memory mapping).
|
||||
|
||||
Searching rules, searching for property names, will loop the internal object `_tables` (it will grow after join query), and check the main table first until the same field is found.
|
||||
|
||||
For example:
|
||||
|
||||
Suppose A, B, and C all have id. When the queried Dto structure is: `Dto {id, a1, a2, b1, b2 }`, `A.id` is mapped. You can also specify the `id = C.id` mapping.
|
||||
|
||||
Dto query only maps default fields (common attributes). For mapping objects, please use:
|
||||
|
||||
> Navigation object: ToList(a => new Dto { Catalog = a.Catalog })
|
||||
|
||||
> Multi table object: ToList((a, b) => new Dto { Catalog = b })
|
||||
|
||||
## ToChunk Returning
|
||||
|
||||
Execute queries and return data in blocks, which can reduce memory overhead. For example, if 100,000 pieces of data are read, 100 pieces of data are returned for processing each time.
|
||||
|
||||
```csharp
|
||||
var testlist1 = fsql.Select<Song>().OrderBy(a => a.Id).ToList();
|
||||
var testlist2 = new List<Song>();
|
||||
fsql.Select<Song>().OrderBy(a => a.Id).ToChunk(100, done => {
|
||||
testlist2.AddRange(done.Object);
|
||||
//done.IsBreak = true; v1.7.0 stop reading
|
||||
});
|
||||
//Here is a demonstration that the final data returned by testlist1 and testlist2 are the same.
|
||||
```
|
||||
|
||||
## ToSql
|
||||
|
||||
All `ToList` can use `ToSql` to return SQL string. There are two options:
|
||||
@@ -145,68 +207,6 @@ fsql.Select<Topic>()
|
||||
//) a
|
||||
```
|
||||
|
||||
## ToChunk
|
||||
|
||||
Execute queries and return data in blocks, which can reduce memory overhead. For example, if 100,000 pieces of data are read, 100 pieces of data are returned for processing each time.
|
||||
|
||||
```csharp
|
||||
var testlist1 = fsql.Select<Song>().OrderBy(a => a.Id).ToList();
|
||||
var testlist2 = new List<Song>();
|
||||
fsql.Select<Song>().OrderBy(a => a.Id).ToChunk(100, done => {
|
||||
testlist2.AddRange(done.Object);
|
||||
//done.IsBreak = true; v1.7.0 stop reading
|
||||
});
|
||||
//Here is a demonstration that the final data returned by testlist1 and testlist2 are the same.
|
||||
```
|
||||
|
||||
## Dto Mapping
|
||||
|
||||
```csharp
|
||||
fsql.Select<Song>().ToList<Dto>();
|
||||
//Case 1: The field with the same property name of Dto and Song is queried, and List<Dto> is returned
|
||||
|
||||
fsql.Select<Song>().ToList(a => new Dto { xxx = a.ext })
|
||||
//Case 2: The field with the same property name of Dto and Song is queried, the mapping ext is adjusted, and List<Dto> is returned
|
||||
|
||||
fsql.Select<Song>().ToList(a => new Song { id = a.id })
|
||||
//Case 3: The type specified by Lambda is the same as the type of Song, only the specified field id is queried, and List<Song> is returned
|
||||
|
||||
fsql.Select<Song>().ToList(a => new { id = a.id })
|
||||
//Case 4: Lambda specifies an anonymous type, only queries the specified field id, and returns List<ANONYMOUS_OBJECT>
|
||||
```
|
||||
|
||||
> Please handle the difference carefully.
|
||||
|
||||
```csharp
|
||||
fsql.Select<Song>().ToList(a => new Dto(a.id))
|
||||
//Case 5: Only query id and return List<Dto>
|
||||
|
||||
fsql.Select<Song>().ToList(a => new Dto(a.id) { xxx = a.ext })
|
||||
//Case 6: Query id, ext and return List<Dto>
|
||||
|
||||
fsql.Select<Song>().ToList(a => new Song(a.id))
|
||||
//Case 7: Query id and return List<Song>
|
||||
|
||||
fsql.Select<Song>().ToList(a => new Song(a.id) { xxx = a.ext })
|
||||
//Case 8: Query id, ext and return List<Song>
|
||||
```
|
||||
|
||||
> All methods of GroupBy are not applicable to DTO mapping rules
|
||||
|
||||
This kind of mapping supports single table/multi-table, mapping before querying data (not to query all fields first and then to memory mapping).
|
||||
|
||||
Searching rules, searching for property names, will loop the internal object `_tables` (it will grow after join query), and check the main table first until the same field is found.
|
||||
|
||||
For example:
|
||||
|
||||
Suppose A, B, and C all have id. When the queried Dto structure is: `Dto {id, a1, a2, b1, b2 }`, `A.id` is mapped. You can also specify the `id = C.id` mapping.
|
||||
|
||||
Dto query only maps default fields (common attributes). For mapping objects, please use:
|
||||
|
||||
> Navigation object: ToList(a => new Dto { Catalog = a.Catalog })
|
||||
|
||||
> Multi table object: ToList((a, b) => new Dto { Catalog = b })
|
||||
|
||||
## API
|
||||
|
||||
| Method | Return | Parameter | Description |
|
||||
|
||||
140
返回数据.md
140
返回数据.md
@@ -75,76 +75,7 @@ List<匿名类> t11 = fsql.Select<Topic>().ToList(a => new {
|
||||
|
||||
参考实现:https://github.com/dotnetcore/FreeSql/issues/528
|
||||
|
||||
## 7、ToSql
|
||||
|
||||
每个 ToList 都可以使用 ToSql 返回 SQL String,有两个选项:
|
||||
|
||||
- FieldAliasOptions.AsIndex(默认) 自动产生 as1, as2, as3 .... 字段别名,可以最大程度防止多表,存在相同字段的问题;
|
||||
- FieldAliasOptions.AsProperty 使用属性名作为字段别名,合适使用二次构造 SQL 再次执行;
|
||||
|
||||
> v3.2.666 开启参数化查询功能后,使用 WithParameters 共享参数化,避免多个查询对象产生相同的参数名称,例如:[UnionAll 联合查询](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2)
|
||||
|
||||
## 8、执行SQL
|
||||
```csharp
|
||||
class xxx {
|
||||
public int Id { get; set; }
|
||||
public string Path { get; set; }
|
||||
public string Title2 { get; set; }
|
||||
}
|
||||
|
||||
List<xxx> t11 = fsql.Ado.Query<xxx>("select * from song");
|
||||
List<(int, string ,string)> t12 = fsql.Ado.Query<(int, string, string)>("select * from song");
|
||||
List<dynamic> t13 = fsql.Ado.Query<dynamic>("select * from song");
|
||||
```
|
||||
|
||||
> 注意:Ado.Query 的实体特性是无效的,比如 [Column(Name = "xxx")] 无效
|
||||
|
||||
## 9、WithSql
|
||||
```csharp
|
||||
fsql.Select<Topic>()
|
||||
.WithSql("select * from Topic where clicks > @val", new { val = 10 })
|
||||
.Page(1, 10)
|
||||
.ToList()
|
||||
//SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime`
|
||||
//FROM (select * from Topic where clicks > @val) a
|
||||
```
|
||||
|
||||
> WithSql 使用多次为 UNION ALL 查询
|
||||
|
||||
> v3.2.666 [UnionAll 联合查询](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2)、[WithTempQuery + FromQuery 嵌套查询](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2)
|
||||
|
||||
> v3.2.666 WithMemory 使用内存数据进行查询
|
||||
|
||||
```csharp
|
||||
var list = new List<Topic>();
|
||||
list.Add(new Topic { ... });
|
||||
list.Add(new Topic { ... });
|
||||
|
||||
fsql.Select<Topic>()
|
||||
.WithMemory(list)
|
||||
.ToList()
|
||||
//SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime`
|
||||
//FROM (
|
||||
// SELECT ...
|
||||
// UNION ALL
|
||||
// SELECT ...
|
||||
//) a
|
||||
```
|
||||
|
||||
## 10、ToChunk
|
||||
|
||||
执行查询,分块返回数据,可减少内存开销。比如读取10万条数据,每次返回100条处理。
|
||||
```csharp
|
||||
var testlist1 = fsql.Select<Song>().OrderBy(a => a.Id).ToList();
|
||||
var testlist2 = new List<Song>();
|
||||
fsql.Select<Song>().OrderBy(a => a.Id).ToChunk(100, done => {
|
||||
testlist2.AddRange(done.Object);
|
||||
//done.IsBreak = true; v1.7.0 停止读取
|
||||
});
|
||||
//这里示范,最终 testlist1 与 testlist2 返回的数据相同。
|
||||
```
|
||||
|
||||
## 11、Dto 映射查询
|
||||
## 7、Dto 映射返回
|
||||
|
||||
```csharp
|
||||
fsql.Select<Song>().ToList<Dto>();
|
||||
@@ -192,6 +123,75 @@ DTO 查询只映射默认字段(普通属性),映射对象请使用:
|
||||
|
||||
> 多表对象:ToList((a, b) => new Dto { Catalog = b })
|
||||
|
||||
## 8、ToChunk 分段返回
|
||||
|
||||
执行查询,分块返回数据,可减少内存开销。比如读取10万条数据,每次返回100条处理。
|
||||
```csharp
|
||||
var testlist1 = fsql.Select<Song>().OrderBy(a => a.Id).ToList();
|
||||
var testlist2 = new List<Song>();
|
||||
fsql.Select<Song>().OrderBy(a => a.Id).ToChunk(100, done => {
|
||||
testlist2.AddRange(done.Object);
|
||||
//done.IsBreak = true; v1.7.0 停止读取
|
||||
});
|
||||
//这里示范,最终 testlist1 与 testlist2 返回的数据相同。
|
||||
```
|
||||
|
||||
## 9、ToSql
|
||||
|
||||
每个 ToList 都可以使用 ToSql 返回 SQL String,有两个选项:
|
||||
|
||||
- FieldAliasOptions.AsIndex(默认) 自动产生 as1, as2, as3 .... 字段别名,可以最大程度防止多表,存在相同字段的问题;
|
||||
- FieldAliasOptions.AsProperty 使用属性名作为字段别名,合适使用二次构造 SQL 再次执行;
|
||||
|
||||
> v3.2.666 开启参数化查询功能后,使用 WithParameters 共享参数化,避免多个查询对象产生相同的参数名称,例如:[UnionAll 联合查询](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2)
|
||||
|
||||
## 10、执行SQL
|
||||
```csharp
|
||||
class xxx {
|
||||
public int Id { get; set; }
|
||||
public string Path { get; set; }
|
||||
public string Title2 { get; set; }
|
||||
}
|
||||
|
||||
List<xxx> t11 = fsql.Ado.Query<xxx>("select * from song");
|
||||
List<(int, string ,string)> t12 = fsql.Ado.Query<(int, string, string)>("select * from song");
|
||||
List<dynamic> t13 = fsql.Ado.Query<dynamic>("select * from song");
|
||||
```
|
||||
|
||||
> 注意:Ado.Query 的实体特性是无效的,比如 [Column(Name = "xxx")] 无效
|
||||
|
||||
## 11、WithSql
|
||||
```csharp
|
||||
fsql.Select<Topic>()
|
||||
.WithSql("select * from Topic where clicks > @val", new { val = 10 })
|
||||
.Page(1, 10)
|
||||
.ToList()
|
||||
//SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime`
|
||||
//FROM (select * from Topic where clicks > @val) a
|
||||
```
|
||||
|
||||
> WithSql 使用多次为 UNION ALL 查询
|
||||
|
||||
> v3.2.666 [UnionAll 联合查询](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2)、[WithTempQuery + FromQuery 嵌套查询](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2)
|
||||
|
||||
> v3.2.666 WithMemory 使用内存数据进行查询
|
||||
|
||||
```csharp
|
||||
var list = new List<Topic>();
|
||||
list.Add(new Topic { ... });
|
||||
list.Add(new Topic { ... });
|
||||
|
||||
fsql.Select<Topic>()
|
||||
.WithMemory(list)
|
||||
.ToList()
|
||||
//SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime`
|
||||
//FROM (
|
||||
// SELECT ...
|
||||
// UNION ALL
|
||||
// SELECT ...
|
||||
//) a
|
||||
```
|
||||
|
||||
## 12、API
|
||||
|
||||
| 方法 | 返回值 | 参数 | 描述 |
|
||||
|
||||
Reference in New Issue
Block a user