开发者

SQL to NHibernate Criteria Query

开发者 https://www.devze.com 2023-01-14 10:08 出处:网络
I\'m using Sharp Architecture for an MVC application and I\'m trying to generate a nhibernate criterion query for the following SQL:

I'm using Sharp Architecture for an MVC application and I'm trying to generate a nhibernate criterion query for the following SQL:

select top 10 p.* from Tab1 p 
                join Tab2 v on p.Id = v.Tab1Fk
                join Tab3 sbu on v.Id = sbu.Tab2Fk
                where sbu.DateTime >= 12/12/2002 00:00:00
                group by p.Id, p.Name, p.CoCode, p.CuCode, p.StCode, p.Pa开发者_StackOverflow中文版rentFk, p.LastGenerated
                order by COUNT(sbu.Id) desc

Currently I've got:

var crit = Session.CreateCriteria<Tab1>();
        crit.SetMaxResults(numberOfRecords);
        crit.CreateCriteria("Tab2", "v", JoinType.InnerJoin)
            .CreateCriteria("Tab3", "sbu", JoinType.InnerJoin)
            .Add(Restrictions.Ge("sbu.DateTime", since))
            .AddOrder(Order.Desc(Projections.Count("sbu.Id")));
        return crit.List<Tab1>();

But it throws errors, can anyone help?


Have you considered using either HQL or NHibernate.Linq? I have several projects under my belt, and I almost always never even bother with the criteria api.

const string hql = @"select p.* 
                    from Tab1 p
                    join p.Tab2 v where p.Tab2.Id = v.Id
                    join p.Tab3 sbu where v.Id = sbu.Tab2.Id
                    and sbu.DateTime >= :since
                    and rownum <= 10
                    group by p.Id, p.Name, p.CoCode
                    order by count(sbu.Id) desc";
                    
var list = Session.CreateQuery(hql)
                .SetParameter("since", DateTime.Now /* your date */)
                .List<Tab1>();

It seems like you're good with SQL, so either of them would seem natural.

Edit: I must say I have never ordered by count(item), so my syntax there might be off. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消