NHibernate的QueryOver与leftjoins(NHibernate QueryOve

2019-10-19 15:56发布

这件事情是让我忙了几天,我希望NHibernate的大师有人能帮助我。 我有一个查询设置这是在SQL Server的工作,我想在NHibernate的相同。 但我的所有尝试(做了很多googeling和计算器浏览)到目前为止还没有。 这里的查询:

Select
  j.id,
  j.title,
  j.company,
  jt.name as category,
  loc.city,
  je.is_assigned,
FROM job j
  LEFT JOIN location loc ON loc.id = j.location
  LEFT JOIN job_tag jt ON jt.job = j.id
    and jt.name in (SELECT name FROM tag WHERE tag_category=''Jobtype'')
  LEFT JOIN job_employee je ON je.job = j.id
    and je.employee_uid = <string>

我宁愿用QueryOver,但会很乐意从你们那里得到建议!

编辑:我确实有到目前为止是非常简单的...

JobEmployee jobEmployee = null;
Location loc = null;
JobTag jobTag = null;

session.QueryOver<Job>()
  .JoinAlias(x => x.location, () => loc)
  .JoinAlias(x => x.tags, () => jobTag, JoinType.LeftOuterJoin,
     Restrictions.On(jobTag.name).IsIn(<subquery>))
  .List();

但也有类似的错误“代表....用不了1个参数”。

最好的问候,马丁

Answer 1:

这里是解决方案的草案。首先子查询:

var subquery = QueryOver
    .Of<Tag>()
    .Where(t => t.TagCategory == "Jobtype")
    .Select(t => t.Name);

而现在,我们将使用在子查询withClause

JobEmployee jobEmployee = null;
Location loc = null;
JobTag jobTag = null;

var list = session
  .QueryOver<Job>()
  .JoinAlias(x => x.location, () => loc)
  .JoinAlias(x => x.tags, () => jobTag, JoinType.LeftOuterJoin,

  // instead of this
  // Restrictions.On(jobTag.name).IsIn(<subquery>))

  // use this
    Subqueries.WhereProperty(() => jobTag.name).In(subquery)
  )
  .List();

所以,我们过滤JOIN使用条款二Subqueries



文章来源: NHibernate QueryOver with leftjoins