且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有多列连接的 Nhibernate 子查询

更新时间:2023-02-02 22:41:42

我想说,这有解决方案.事实上,我们必须使用更复杂的 SQL.我已经在这里深入解释了这种方法:

I would say, that this has solution. We have to use a bit more complex SQL in fact. This approach I've already deeply explained here:

因此,以下只是基于您的子查询草稿的草稿.我们正在做的实际上是创建两个子选择(检查预期的 SQL 此处)

So, below is just a draft based on your subquery draft. What we are doing, is creating two subselects in fact (check the intended SQL here)

PlanVersion planVersion = null;

// the most INNER SELECT
var maxSubquery = QueryOver.Of<PlanVersion>()
   .SelectList(l => l
    .SelectGroup(item => item.ParentPlan.Id)
    .SelectMax(item => item.ActiveFromDate)
    )
    // WHERE Clause
   .Where(item => item.ParentPlan.Id == planVersion.ParentPlan.Id)
   // HAVING Clause
   .Where(Restrictions.EqProperty(
      Projections.Max<PlanVersion>(item => item.ActiveFromDate),
      Projections.Property(() => planVersion.ActiveFromDate)
    ));

// the middle SELECT
var successSubquery = QueryOver.Of<PlanVersion>(() => planVersion )
    // the Plan ID
    .Select(pv => pv.ParentPlan.Id)
    .WithSubquery
    .WhereExists(maxSubquery)
    ;

有了这个子查询,我们可以要求计划本身:

having this subqueries, we can ask for plan itself:

// the most outer SELECT
var query = session.QueryOver<Plan>()
    .WithSubquery
    .WhereProperty(p => p.Id)
    .In(successSubquery)
    .List<Plan>();

可能有一些小的错别字,但草案应该给你明确的答案如何...

There could be some minor typos, but the draft should give you clear answer how to...