mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-03-12 00:50:59 +08:00
update
11
贪婪加载.md
11
贪婪加载.md
@@ -19,7 +19,6 @@ IncludeMany 贪婪加载集合的导航属性,其实是分两次查询,在 T
|
||||
|
||||
```csharp
|
||||
Select<Tag>().IncludeMany(a => a.Songs).ToList();
|
||||
//这是 ManyToMany 关系的贪婪加载
|
||||
```
|
||||
|
||||
IncludeMany 有第二个参数,可以进行二次查询前的修饰工作。
|
||||
@@ -29,11 +28,11 @@ Select<Tag>().IncludeMany(a => a.Songs,
|
||||
then => then.Where(song => song.User == "admin")).ToList();
|
||||
```
|
||||
|
||||
然后,其实在 then 那里,还可以继续进行向下 Include/IncludeMany。只要你喜欢,向下 100 层都没问题。
|
||||
其实在 then 那里,还可以继续进行向下 Include/IncludeMany。只要你喜欢,向下 100 层都没问题。
|
||||
|
||||
## 3、变异
|
||||
|
||||
变异的 IncludeMany 使用的集合属性未配置导航,也可以贪婪加载。
|
||||
没有配置导航关系,也可以贪婪加载。
|
||||
|
||||
```csharp
|
||||
Select<Tag>().IncludeMany(a => a.TestManys.Where(b => b.TagId == a.Id));
|
||||
@@ -45,7 +44,7 @@ Select<Tag>().IncludeMany(a => a.TestManys.Where(b => b.TagId == a.Id));
|
||||
Select<Tag>().IncludeMany(a => a.TestManys.Take(10));
|
||||
```
|
||||
|
||||
设置子集合返回部分字段,避免子集合字段过多的问题。
|
||||
子集合返回部分字段,避免字段过多的问题。
|
||||
|
||||
```csharp
|
||||
Select<Tag>().IncludeMany(a => a.TestManys.Select(b => new TestMany { Title = b.Title ... }));
|
||||
@@ -53,16 +52,12 @@ Select<Tag>().IncludeMany(a => a.TestManys.Select(b => new TestMany { Title = b.
|
||||
|
||||
## 4、IncludeMany 扩展方法
|
||||
|
||||
前面介绍 ISelect 中进行贪婪加载集合属性数据,并且只能使用 IncludeMany + ToList() 组合(不能使用指定字段查询)。
|
||||
|
||||
当主数据已存在内存中,子数据怎么加载?所以我们增加了 List\<T\> 扩展方法,示例如下:
|
||||
|
||||
```csharp
|
||||
new List<Song>(new[] { song1, song2, song3 }).IncludeMany(fsql, a => a.Tags);
|
||||
```
|
||||
|
||||
这个扩展方法(IncludeMany),参数基本一致(除了需要额外传递 IFreeSql),功能也一致(包括前面提到的变异)。
|
||||
|
||||
## 5、IncludeMany 两种方式对比
|
||||
|
||||
方式一(IncludeMany 扩展方法):
|
||||
|
||||
Reference in New Issue
Block a user