简体   繁体   English

IQueryable的Linq查询与多个连接在内部级

[英]IQueryable Linq query with multiple joins at inner level

EDIT: forgot to say I'm using Fluent NHibernate, even though the tag could hint about it anyway. 编辑:忘记说我正在使用Fluent NHibernate,即使该标签仍然可以暗示它。

I have these entity classes: 我有这些实体类:

class OuterLevel
{
    ICollection<MidLevel> mid_items;

    ... other properties
}

class MidLevel
{
    OuterLevel parent;
    Inner1 inner1;
    Inner2 inner2;

    ... other properties
}

class Inner1
{
    int id;
    string description;
}

class Inner2
{
    int id;
    string description;
}

I need to build a Linq query that returns a list of OuterLevel objects with all children populated properly. 我需要构建一个Linq查询,该查询返回OuterLevel对象的列表,并正确填充所有子项。 Supposing all mappings are correct and working, the hard part I'm finding here is that the resulting query should be something like 假设所有映射都是正确的并且可以正常工作,那么我在这里很难找到的部分就是查询结果应该类似于

SELECT * FROM OuterLevelTable OLT INNER JOIN MidLevelTable MLT ON (MLT.parentID = OLT.ID) INNER JOIN
    Inner1Table ON (MLT.Inner1ID = Inner1Table.ID) INNER JOIN
    Inner2Table ON (MLT.Inner2ID = Inner2Table.ID)
 WHERE (Inner1Table.someproperty1 = somevalue1) AND (Inner2Table.someproperty2 = somevalue2)

The main problem is that two joins start from MidLevel object downward the hierarchy, so I cannot figure out which Fetch and FetchMany combination can be used without having the resulting query join two times the MidLevelTable, such as the following does: 主要问题是两个联接从MidLevel对象开始向下沿层次结构进行,因此我无法弄清楚可以使用哪种Fetch和FetchMany组合,而不必使结果查询联接两次MidLevelTable,如下所示:

  return All().FetchMany(x => x.mid_items).ThenFetch(x => x.inner1).FetchMany(x => x.mid_items).ThenFetch(x => x.inner2);

I would like to return a IQueryable that can be further filtered, so I would prefer avoiding Query and QueryOver. 我想返回一个可以进一步过滤的IQueryable,所以我希望避免使用Query和QueryOver。

Thanks in advance, Mario 预先感谢,马里奥

what you want is not possible because when you filter on the joined tables the resulting records are not enough to populate the collections anyway. 所需的内容是不可能的,因为在连接的表上进行过滤时,结果记录仍然不足以填充集合。 You better construct the query in one place to further tune it or set the collection batch size to get down SELECT N+1. 您最好将查询构造在一个地方以进一步优化它,或者将集合批处理大小设置为SELECT N + 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM