C# NH QueryOver-在子查询中使用主查询的属性
我正在尝试将以下SQL转换为QueryOver:C# NH QueryOver-在子查询中使用主查询的属性,c#,.net,nhibernate,queryover,nhibernate-criteria,C#,.net,Nhibernate,Queryover,Nhibernate Criteria,我正在尝试将以下SQL转换为QueryOver: Select 1 From myTable mt Where mt.ForeignKey in (select ID from otherTable ot where ot.ID = R.ID) 我想在EXISTS/NOT EXISTS语句中使用此子查询,如: select * from table R where .... AND EXISTS (query above) 目前我有一些类似于: mainQuery.WithSubquer
Select 1
From myTable mt
Where mt.ForeignKey in (select ID from otherTable ot where ot.ID = R.ID)
我想在EXISTS/NOT EXISTS语句中使用此子查询,如:
select * from table R where .... AND EXISTS (query above)
目前我有一些类似于:
mainQuery.WithSubquery.WhereExists(QueryOver.Of<myTable>()
.Where(mt => mt.ForeignKey)
.WithSubquery.IsIn(QueryOver.Of<otherTable>().Where(c => c.Id == R.SomeId)));
mainQuery.WithSubquery.WhereExists(QueryOver.Of())
.其中(mt=>mt.ForeignKey)
.WithSubquery.IsIn(QueryOver.Of(),其中(c=>c.Id==R.SomeId));
我将此查询创建为要连接到主查询的子查询。
问题是,别名为R的表是主查询调用的表,我不知道如何访问表(NHibernate模型)R的列(在上面的查询中不可访问),因此我的问题是:
如何从主查询中获取值并在子查询中使用它们。我认为这只能通过内联创建子查询(如mainQuery.WithSubquery.Where(..)或smth.similor)实现,但我看不出最好的方法是什么。谢谢你的帮助
提前谢谢 诀窍是为父查询使用正确的别名:
// the alias
myTable R = null;
mainQuery
.WithSubquery
.WhereExists(QueryOver
.Of<myTable>( () => R) // the Alias in place
.Where(mt => mt.ForeignKey)
.WithSubquery.IsIn(QueryOver.Of<otherTable>().Where(c => c.Id == R.SomeId)));
//别名
myTable R=null;
主查询
.带subquery
.存在于何处(查询)
.Of(()=>R)//别名已就位
.其中(mt=>mt.ForeignKey)
.WithSubquery.IsIn(QueryOver.Of(),其中(c=>c.Id==R.SomeId));
注意,不能完全确定mainQuery部分,但这里的解决方案通常是这样的:
// I. the outer query ALIAS
Employee emplyoee = null;
// II. the subquery - using the alias
var subQuery = QueryOver.Of<Contact>()
.Select(x => x.ID)
.Where(x => x.Related.ID == emplyoee.ID); // use alias
// III. declare the outer query and use the above alias
var query = session.QueryOver<Employee>(() => emplyoee) // declare alias
.WithSubquery
.WhereExists(subQuery); // put both together
//I.外部查询别名
员工受雇人=空;
//二,。子查询-使用别名
var subQuery=QueryOver.Of()
.选择(x=>x.ID)
。其中(x=>x.Related.ID==employee.ID);//使用别名
//III.声明外部查询并使用上述别名
var query=session.QueryOver(()=>employee)//声明别名
.带subquery
.WhereExists(子查询);//把两者放在一起
另请查看更多想法谢谢您的别名提示!在您的帮助下,通过查看代码库中的其他代码,我成功地解决了这个问题!很好,我看不到;)享受NHibernate,很棒的工具;)