mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-10 02:10:55 +08:00
Updated Greed Loading (markdown)
@@ -30,45 +30,45 @@ Select<Tag>().IncludeMany(a => a.Songs,
|
||||
then => then.Where(song => song.User == "admin")).ToList();
|
||||
```
|
||||
|
||||
其实在 then 那里,还可以继续进行向下 Include/IncludeMany。只要你喜欢,向下 100 层都没问题。
|
||||
In fact, in `Then`, you can continue to use `Include`/`IncludeMany`. As long as you like it, it’s okay to go down 100 levels.
|
||||
|
||||
## 3、变异
|
||||
## Mutations
|
||||
|
||||
没有配置导航关系,也可以贪婪加载。
|
||||
It can also be greedily loaded without configuring the navigation relationship.
|
||||
|
||||
```csharp
|
||||
Select<Tag>().IncludeMany(a => a.TestManys.Where(b => b.TagId == a.Id));
|
||||
```
|
||||
|
||||
只查询每项子集合的前几条数据,避免像EfCore加载所有数据导致IO性能低下(比如某商品下有2000条评论)。
|
||||
Only query the first few pieces of data in each sub-collection to avoid poor IO performance caused by loading all data like EfCore (for example, there are 2000 comments under a product).
|
||||
|
||||
```csharp
|
||||
Select<Tag>().IncludeMany(a => a.TestManys.Take(10));
|
||||
```
|
||||
|
||||
子集合返回部分字段,避免字段过多的问题。
|
||||
The sub-collection returns a part of the fields to avoid the problem of too many fields.
|
||||
|
||||
```csharp
|
||||
Select<Tag>().IncludeMany(a => a.TestManys.Select(b => new TestMany { Title = b.Title ... }));
|
||||
```
|
||||
|
||||
## 4、IncludeMany 扩展方法
|
||||
## IncludeMany Extensions
|
||||
|
||||
当主数据已存在内存中,子数据怎么加载?所以我们增加了 List\<T\> 扩展方法,示例如下:
|
||||
When the main data already exists in the memory, how to load the sub-data? So we added the List\<T\> extension method, the example is as follows:
|
||||
|
||||
```csharp
|
||||
new List<Song>(new[] { song1, song2, song3 }).IncludeMany(fsql, a => a.Tags);
|
||||
```
|
||||
|
||||
## 5、IncludeMany 两种方式对比
|
||||
## Comparison of the Two Ways of IncludeMany
|
||||
|
||||
方式一(IncludeMany 扩展方法):
|
||||
**Way 1: IncludeMany extensions**
|
||||
|
||||
```csharp
|
||||
var list111 = fsql.Select<SysModule>()
|
||||
.Page(1, 10)
|
||||
.ToList(a => new { Id = a.Id }) //查询数据 id
|
||||
.Select(a => new SysModule { Id = a.Id }).ToList() //内存操作
|
||||
.ToList(a => new { Id = a.Id }) //Query data id
|
||||
.Select(a => new SysModule { Id = a.Id }).ToList() //Memory operation
|
||||
.IncludeMany(fsql, a => a.Permissions, then => then.Include(a => a.Button));
|
||||
```
|
||||
|
||||
@@ -86,7 +86,7 @@ WHERE ((a."SysModuleId") in ('menu1','menu2'))
|
||||
|
||||
---
|
||||
|
||||
方式二(直接 IncludeMany + ToList):
|
||||
**Way 2: Directly IncludeMany + ToList**
|
||||
|
||||
```csharp
|
||||
var list222 = fsql.Select<SysModule>()
|
||||
@@ -107,7 +107,7 @@ LEFT JOIN "SysModuleButton" a__Button ON a__Button."Id" = a."SysModuleButtonId"
|
||||
WHERE ((a."SysModuleId") in ('menu1','menu2'))
|
||||
```
|
||||
|
||||
案例:查询 Vod 表,分类1、分类2、分类3 各10条数据
|
||||
Case: Query Vod table, 10 data for each of category 1, category 2, and category 3
|
||||
|
||||
```csharp
|
||||
class Vod {
|
||||
@@ -115,7 +115,7 @@ class Vod {
|
||||
public int TypeId { get; set; }
|
||||
}
|
||||
|
||||
//定义临时类,也可以是 Dto 类
|
||||
//Define a temporary class, it can also be a DTO
|
||||
class Dto {
|
||||
public int TypeId { get; set; }
|
||||
public List<Vod> Vods { get; set; }
|
||||
@@ -124,7 +124,7 @@ class Dto {
|
||||
var dto = new [] { 1,2,3 }.Select(a => new Dto { TypeId = a }).ToList();
|
||||
dto.IncludeMany(fsql, d => d.Vods.Take(10).Where(vod => vod.TypeId == d.TypeId));
|
||||
|
||||
//执行后,dto 每个元素.Vods 将只有 10条记录
|
||||
//After execution, each element.Vods of DTO will only have 10 records
|
||||
```
|
||||
|
||||
## Reference
|
||||
|
||||
Reference in New Issue
Block a user