Include 和 ThenInclude 包含多层次的集合
Include and ThenInclude with multiple levels of collections
我正在尝试查询具有多个级别的集合以及单个级别的多个集合的实体。我正在使用 Include()
和 ThenInclude()
,但没有取得太大的成功。我发现的示例在同一级别上没有多个集合,而且我没有运气将该技术应用于我的用例。
这是我的实体的简化图示。带[]的是集合:
Home
Areas[]
Area
Name
Categories[]
Name
Recommendations[]
Subcategories[]
Name
Recommendations[]
Area
Name
Categories[]
Name
Recommendations[]
Subcategories[]
Name
Recommendations[]
我已经走到这一步了:
result = Home
.Include(x => x.Areas)
.ThenInclude(a => a.Categories)
.ThenInclude(c => c.Subcategories)
.ThenInclude(s => s.Recommendations)
但是,这错过了 Categories[].Recommendations[]
集合。这是因为同一级别有两个集合(Recommendations[]
和Subcategories[]
)。
对于构建此查询以便获得所需结果的方法有何建议?
谢谢。
您必须为每个级别调用 Include
:
result = Home
.Include(x => x.Areas)
.ThenInclude(a => a.Categories)
.ThenInclude(c => c.Subcategories)
.ThenInclude(s => s.Recommendations)
.Include(x => x.Areas)
.ThenInclude(a => a.Recommendations)
您必须按如下方式编写查询:
result = Home
.Include(x => x.Areas)
.ThenInclude(a => a.Categories)
.ThenInclude(s => s.Recommendations)
.Include(x => x.Areas)
.ThenInclude(c => c.Subcategories)
我正在尝试查询具有多个级别的集合以及单个级别的多个集合的实体。我正在使用 Include()
和 ThenInclude()
,但没有取得太大的成功。我发现的示例在同一级别上没有多个集合,而且我没有运气将该技术应用于我的用例。
这是我的实体的简化图示。带[]的是集合:
Home
Areas[]
Area
Name
Categories[]
Name
Recommendations[]
Subcategories[]
Name
Recommendations[]
Area
Name
Categories[]
Name
Recommendations[]
Subcategories[]
Name
Recommendations[]
我已经走到这一步了:
result = Home
.Include(x => x.Areas)
.ThenInclude(a => a.Categories)
.ThenInclude(c => c.Subcategories)
.ThenInclude(s => s.Recommendations)
但是,这错过了 Categories[].Recommendations[]
集合。这是因为同一级别有两个集合(Recommendations[]
和Subcategories[]
)。
对于构建此查询以便获得所需结果的方法有何建议?
谢谢。
您必须为每个级别调用 Include
:
result = Home
.Include(x => x.Areas)
.ThenInclude(a => a.Categories)
.ThenInclude(c => c.Subcategories)
.ThenInclude(s => s.Recommendations)
.Include(x => x.Areas)
.ThenInclude(a => a.Recommendations)
您必须按如下方式编写查询:
result = Home
.Include(x => x.Areas)
.ThenInclude(a => a.Categories)
.ThenInclude(s => s.Recommendations)
.Include(x => x.Areas)
.ThenInclude(c => c.Subcategories)