mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-04 23:40:53 +08:00
Updated Parent Child Relationship Query (markdown)
@@ -1,16 +1,16 @@
|
||||
[中文](%e6%9f%a5%e8%af%a2%e7%88%b6%e5%ad%90%e5%85%b3%e7%b3%bb) | **English**
|
||||
|
||||
无限级分类(父子)是一种比较常用的表设计,每种设计方式突出优势的同时也带来缺陷,如:
|
||||
Unlimited classification (father and child) is a commonly used table design. Each design method highlights advantages but also brings defects, such as:
|
||||
|
||||
- 方法1:表设计中只有 parent_id 字段,困扰:查询麻烦(本文可解决);
|
||||
- 方法2:表设计中冗余子级id便于查询,困扰:添加/更新/删除的时候需要重新计算;
|
||||
- 方法3:表设计中存储左右值编码,困扰:同上;
|
||||
- Way 1:There is only the `parent_id` field in the table design, which is **troublesome**: query trouble (this article can solve it).
|
||||
- Way 2:The redundant child id in the table design is easy to query, and it is **troublesome**: it needs to be recalculated when insert/update/delete;
|
||||
- Way 3:The left and right value codes are stored in the table design, **troublesome**: the same as above;
|
||||
|
||||
方法1设计最简单,本文解决它的递归查询问题,让使用透明化。
|
||||
Way 1: The design is the simplest. This article solves its recursive query problem and makes the use transparent.
|
||||
|
||||
## Parent-child Navigation Properties
|
||||
|
||||
FreeSql 导航属性之中,有针对父子关系的设置方式,如下:
|
||||
Among the FreeSql navigation properties, there are settings for the parent-child relationship, as follows:
|
||||
|
||||
```csharp
|
||||
public class Area
|
||||
@@ -28,17 +28,19 @@ public class Area
|
||||
}
|
||||
```
|
||||
|
||||
定义 Parent 属性,在表达式中可以这样:
|
||||
Define the Parent property, in the expression can be like this:
|
||||
|
||||
```csharp
|
||||
fsql.Select<Area>().Where(a => a.Parent.Parent.Parent.Name == "中国").First();
|
||||
```
|
||||
|
||||
定义 Childs 属性,在表达式中可以这样(子查询):
|
||||
Define the Childs attribute, in the expression (subquery):
|
||||
|
||||
```csharp
|
||||
fsql.Select<Area>().Where(a => a.Childs.AsSelect().Any(c => c.Name == "北京")).First();
|
||||
```
|
||||
|
||||
定义 Childs 属性,还可以使用[【级联保存】](%E8%81%94%E7%BA%A7%E4%BF%9D%E5%AD%98)、[【贪婪加载】](%e8%b4%aa%e5%a9%aa%e5%8a%a0%e8%bd%bd) 等等操作。
|
||||
To define the Childs property, you can also use [Cascade Saving](Cascade-Saving), [Greed Loading](Greed-Loading) and so on.
|
||||
|
||||
```csharp
|
||||
fsql.Delete<Area>().Where("1=1").ExecuteAffrows();
|
||||
@@ -65,7 +67,7 @@ repo.Insert(new Area
|
||||
|
||||
## ToTreeList
|
||||
|
||||
配置好父子属性之后,就可以这样用了:
|
||||
After configuring the parent-child properties, you can use it like this:
|
||||
|
||||
```csharp
|
||||
var t1 = fsql.Select<Area>().ToTreeList();
|
||||
|
||||
Reference in New Issue
Block a user