使用 nHibernate 和 QueryOver 如何连接 3 个表

发布于 2024-11-29 23:51:14 字数 1356 浏览 1 评论 0原文

背景: 给定 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

我做错了什么?

BACKGROUND:
Given 3 tables

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

I want to accomplish this sort of SQL query using QueryOver

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  

I've tried the following:

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)

which works just fine to get data from vTable

however, when I add the 2nd table

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)

I get the error

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

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2024-12-06 23:51:14
.JoinQueryOver<pTable>(p => p.pId)

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

.JoinQueryOver<pTable>(p => p.pId)

has to point to a mapped entity or collection not it an id, if you can't map it in the hbm. And also the JoinQueryOver will return pTables not vTables, you might want to use JoinAlias instead if you wan to retain the return type to be a list of vTables, but if all you want is that projection make sure you add aliases the QueryOver and JoinQueryOver calls

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文