将此SQL查询转换为NHibernate Criteria Queries

时间:2011-08-28 15:33:22

标签: sql nhibernate fluent-nhibernate

需要将此sql查询转换为nhibernate准则语言...我该怎么做?

 select 
     * 
 from Feeds where Id = 
 (select MAX(Id) from Feeds where DataId = [VARIABLE PARAMETER])

1 个答案:

答案 0 :(得分:2)

Feed feed = _session.CreateCriteria<Feed>()
    .Add(Restrictions.Eq("DataId", "[VARIABLE PARAMETER]"))
    .AddOrder(new Order("_id", false))
    .SetMaxResults(1)
    .List<Feed>()
    .FirstOrDefault();

这将导致类似:

select TOP 1 * from Feeds where DataId = ??? Order By Id desc

从性能的角度来看,应该没有difference