【问题标题】:Using nHibernate and QueryOver how can I join 3 tables使用 nHibernate 和 QueryOver 如何加入 3 个表
【发布时间】:2011-08-17 05:06:06
【问题描述】:

背景: 给定3张桌子

results contains 2 columns vId and pId  
vTable contains 2 columns vId and data  
pTable contains 2 columns pId and data  

我想使用 QueryOver 完成这种 SQL 查询

SELECT v.data, p.data  
from results r  
inner join vTable v on r.vId = v.vId   
inner join pTable p on r.pId = p.pId  

我尝试了以下方法:

var res = GetResults(some parameters)
            .Select(x => x.vId
            .Select(x => x.pID);

var dataset = session.QueryOver<vTable>()
                .WithSubquery.WhereProperty(v => v.vId).In(res)
                .Select(v => v.vId)
                .Select(v => v.data)

从 vTable 获取数据效果很好

但是,当我添加第二个表时

var dataset = session.QueryOver<vTable>()
                .WithSubquery.WhereProperty(v => v.vId).In(res)
                .JoinQueryOver<pTable>(p => p.pId)
                .WithSubquery.WhereProperty(p => p.pId).In(res)
                .Select(v => v.vId)
                .Select(v => v.data)
                .Select(p => p.pId)
                .Select(p => p.data)

我得到了错误

Delegate 'System.Func<System.Collections.Generic.IEnumerable<pTable>>' does not take 1 arguments

我做错了什么?

【问题讨论】:

    标签: nhibernate fluent-nhibernate queryover


    【解决方案1】:
    .JoinQueryOver<pTable>(p => p.pId)
    

    如果不能在 hbm 中映射它,则必须指向映射的实体或集合而不是 id。而且 JoinQueryOver 将返回 pTables 而不是 vTables,如果您希望将返回类型保留为 vTables 列表,您可能希望使用 JoinAlias,但如果您想要的只是该投影,请确保添加别名 QueryOver 和 JoinQueryOver 调用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多