嘿 - 我正在努力弄清楚如何使用NHibernate ICriteria(多标准?)来编写以下内容:
(这是一个查询,以获取最后一天在表格中按人气排序的名字列表)
select firstname,count(firstname) as occurances from registrants
where timestamp between DateAdd(day,-1, GetDate()) and getdate()
group by firstname
order by count(firstname) desc
另外,鉴于这只是一个表中的几列,不包括ID,NHibernate需要ID的对象,什么是“假”ID的最简单的方法,所以我可以得到结果?
答案 0 :(得分:4)
您需要使用投影和变换器来执行此操作。这是一些背景信息http://nhibernate.info/doc/nh/en/index.html#querycriteria-projection
var criteria = Session.CreateCriteria<Registrant>()
.Add(Restrictions.Between("Timestamp", DateTime.Now.AddDays(-1), DateTime.Now)
.AddOrder(Order.Desc(Projections.Count("FirstName")))
.SetProjection(Projections.ProjectionList()
.Add(Projections.GroupProperty("FirstName"), "FirstName")
.Add(Projections.Count("FirstName"), "Occurances")
.SetResultTransformer(Transformers.AliasToBean<FirstNameOccurance>());
criteria.List<FirstNameOccurance>();
您需要创建一个名为FirstNameOccurance的类,该类具有2个名为FirstName和Occurances的属性。