加入嵌套查询NHibernate

时间:2011-06-29 15:54:32

标签: nhibernate criteria

如何使用条件或QueryOver

在NHibernate中执行此查询
select r.relationshipidentifier, r.publicrelationshipid
from relationship r 
inner join (
    select max(asofdate) as asofdate, s.relationshipidentifier
    from relationship s
    group by s.relationshipidentifier
) m
on r.asofdate = m.asofdate and r.relationshipidentifier = m.relationshipidentifier

假设

public class Relationship {
    public virtual Guid Id { get; set; }
    public virtual DateTime AsOfDate { get; set; }
    public virtual Guid RelationshipIdentifier { get; set; }
    public virtual Guid PublicRelationshpId { get; set; }
}

1 个答案:

答案 0 :(得分:0)

内连接可以用子查询来描述 事情的影响 -

QueryOver.Of<Relationship>()
.SelectList(list =>
    list.SelectMax(r => r.AsOfDate),
    list.GroupProperty(r=> r.RelationshipIdentifier)
)

并且您可以使用QueryOver的WithSubquery方法来连接到该子查询。 有关QueryOver API的详细信息,请参阅this article