之前我曾经看过这个问题,但它从来没有得到解决 - 原始海报也不是很清楚,所以我再次尝试更清晰了。
鉴于以下模拟结构...
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对象上不存在,所以我最终得到了这样的对象。
=======“containers / 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可以包含在同一个请求中?