【问题标题】:Nhibernate query over with multiple fetch and multiple conditions使用多个获取和多个条件进行休眠查询
【发布时间】:2023-04-01 07:35:01
【问题描述】:

当像这样使用实体框架linq 时,如何使用 NHibernate QueryOver 获得相同的结果。

var result = items
   .include("subEntity1")
   .include("subEntity2")
   .include("subEntity3")
   .where(...).skip(x).take(y); 

【问题讨论】:

    标签: c# .net nhibernate


    【解决方案1】:

    QueryOver 的语法可能如下所示:

    var query = session.QueryOver<MyEntity>()
        // force the collection inclusion
        .Fetch(x => x.Collection1).Eager
        .Fetch(x => x.Collection2).Eager
        ...
        // force the relations inclusion
        .Fetch(x => x.SubEntity1).Eager
        .Fetch(x => x.SubEntity2).Eager
        ...
        // paging
        .Skip(x)
        .Take(y);
    
    var list = query
         .List<MyEntity>();
    

    来源:

    【讨论】:

    • 我尝试了这个,但结果中出现了重复的实体
    • 是的,这就是 Fetch - 集合的影响。它总是导致carthesian产品。合适的方式其实就是做NOT Fetch。将您的映射更改为使用批量大小,这将以超过 1 个查询结束,但仍然非常有效。请检查stackoverflow.com/a/20970816/1679310stackoverflow.com/a/19287008/1679310
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 2014-03-30
    • 2013-02-13
    • 2015-08-14
    • 2019-01-15
    相关资源
    最近更新 更多