SQL 到 NHibernate 标准查询
我正在为 MVC 应用程序使用 Sharp Architecture,并尝试为以下 SQL 生成 nhibernate 标准查询:
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.ParentFk, p.LastGenerated
order by COUNT(sbu.Id) desc
目前我已经得到:
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>();
但它会抛出错误,任何人都可以帮忙吗?
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.ParentFk, 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否考虑过使用 HQL 或 NHibernate。 Linq?我手下有几个项目,但我几乎从来不关心标准 api。
看起来您很擅长 SQL,所以它们中的任何一个看起来都很自然。
编辑:我必须说我从来没有按 count(item) 排序,所以我的语法可能会关闭。 :)
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.
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. :)