NH QueryOver - use properties of main query in subquery

末鹿安然 提交于 2019-12-02 06:23:16
Radim Köhler

The trick is to use proper alias, for the parent query:

// 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)));

Note, not fully sure about the mainQuery part, but the solution in general here is like this:

// 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

Also check this for more ideas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!