需要将选择多个值的子查询转换为nhibernate查询(条件或hql)

需要将选择多个值的子查询转换为nhibernate查询(条件或hql),nhibernate,subquery,hql,criteria,Nhibernate,Subquery,Hql,Criteria,我需要将以下查询转换为nhibernate: SELECT o.* FROM orders o INNER JOIN ( -- get the most recent orders based on end_date (this implies the same order can exist in the orders table more than once) SELECT o2.order_id, MAX(o2.end_date) AS max_end

我需要将以下查询转换为nhibernate:

SELECT  o.* 
FROM orders o
INNER JOIN 
(
        -- get the most recent orders based on end_date (this implies the same order can exist in the orders table more than once)
        SELECT o2.order_id, MAX(o2.end_date) AS max_end_date 
        FROM orders o2
        GROUP BY o2.order_id
) most_recent_orders 
                ON o.order_id=most_recent_orders.order_id
                AND o.end_date=most_recent_orders.max_end_date
-- of the most recent orders, get the ones that are complete                
WHERE o.is_complete=1
我知道hql不支持加入子查询,这就是为什么这不起作用。我不能使用“in”语句,因为子查询正在选择2个值。我尝试使用hibernate文档中的建议:

但这引发了一个错误,因为Sql Server不喜欢在“in”语句中包含多个值


任何帮助都将不胜感激!我对使用条件或Hql的解决方案持开放态度。

这是使用子查询来实现相同的解决方案

var maxDateQuery = DetachedCriteria.For<Order>()
    .Add(Restrictions.PropertyEq("OrderId", "order.OrderId"))
    .SetProjection(Projections.Property("EndDate"));

var results = session.CreateCriteria<Order>("order")
    .Add(Subqueries.Eq("EndDate",maxDateQuery))
    .Add(Restrictions.Eq("IsComplete", true))
    .List<Order>();
var maxDateQuery=DetachedCriteria.For()
.Add(Restrictions.PropertyEq(“OrderId”、“order.OrderId”))
.SetProjection(Projections.Property(“EndDate”);
var results=session.CreateCriteria(“订单”)
.Add(subquerys.Eq(“EndDate”,maxDateQuery))
.Add(Restrictions.Eq(“IsComplete”,true))
.List();

order.OrderId“来自哪里?我在查询中的任何地方都没有看到名为“order”的别名?现在它抛出一个异常,因为它将“order.OrderId”视为字符串文本,而不是插值它。错误:NHibernate.QueryException:NHibernate.Criteria.SimpleExpression中的类型不匹配:Id应为类型System.Guid,实际类型System.String
var maxDateQuery = DetachedCriteria.For<Order>()
    .Add(Restrictions.PropertyEq("OrderId", "order.OrderId"))
    .SetProjection(Projections.Property("EndDate"));

var results = session.CreateCriteria<Order>("order")
    .Add(Subqueries.Eq("EndDate",maxDateQuery))
    .Add(Restrictions.Eq("IsComplete", true))
    .List<Order>();