我以前曾看过这个问题,但从未得到解决 - 原始海报并不是很清楚,所以我再次尝试更清楚一点。
鉴于以下模拟结构...
public class Container {
public string Id { get; set; }
public List<Entity> Entities { get; set; }
}
public class Entity {
public string Id { get; set; }
public List<string> Sub_Entities { get; set; }
}
public class Sub_Entity {
public string Id { get; set; }
public List<string> Sub_Entities { get; set; }
}
这意味着 Sub_Entity 永远不会被非规范化,因此它的相关 Id 不存在于实际的 JSON 对象上,所以我最终得到了这样的对象。
=======“容器/1”=======
{
"Entities" : [
{
"Id" : "entities/1",
"Sub_Entities" : [
"sub_entities/1",
"sub_entities/2",
"sub_entities/3"
]
}
]
}
=======“sub_entities/1”=======
{
"Sub_Entities" : [
"sub_entities/4",
"sub_entities/5",
"sub_entities/6"
]
}
=======“sub_entities/2”=======
{
"Sub_Entities" : [
"sub_entities/7",
"sub_entities/8",
"sub_entities/9"
]
}
=======“sub_entities/3”=======
{
"Sub_Entities" : [
"sub_entities/10",
"sub_entities/11",
"sub_entities/12"
]
}
现在我想真正优化从数据库中提取所有内容的方式。所以我首先将 sub_entities 包含在初始容器中。
我已经这样做了:
var container = DocumentSession
.Include("Entities,Id")
.Include("Entities,Sub_Entities")
.Load<Container>("containers/1");
我还使用谓词通过以下方式完成了它。
var container = DocumentSession
.Include<Container>(n => n.Entities.SelectMany( y => y.Id ) )
.Include<Container>(n => n.Entities.SelectMany( y => y.Sub_Entities ))
.Load<Container>("containers/1");
这让我在一个请求中获得了初始的 sub_entities 集,但是我可以做些什么来更深入地查询以确保嵌套的 sub_entities 可以包含在同一个请求中?