【发布时间】:2014-06-16 10:22:20
【问题描述】:
如何在 nHibernate 中使用 queryover 实现以下查询
查询
SELECT [DepartmentID] ,COUNT(courseId)
FROM [Course]
where [DepartmentID] >1
GROUP BY [DepartmentID]
hAVING COUNT(courseId) = 2 or COUNT(courseId) = 3;
表结构
CREATE TABLE [dbo].[Course](
[CourseID] [int] NOT NULL,
[Title] [nvarchar](100) NOT NULL,
[Credits] [int] NOT NULL,
[DepartmentID] [int] NOT NULL,
我尝试过的 C# 代码
Collection<Course> courseList = new Collection<Course>();
using (var session = NHibernateHelper.OpenSession("ConnectionName1"))
{
Course courseAlias = null;
SimpleExpression e1 = Restrictions.Ge(Projections.Count(Projections.Property(() => courseAlias.CourseID)), 2);
SimpleExpression e2 = Restrictions.Ge(Projections.Count(Projections.Property(() => courseAlias.CourseID)), 3);
var results2 = session.QueryOver<Course>(() => courseAlias)
.Where(e1||e2)
.SelectList(list => list
.SelectGroup(x => x.DepartmentID).WithAlias(() => courseAlias.DepartmentID)
.SelectCount(x => x.CourseID).WithAlias(() => courseAlias.CourseID)
).TransformUsing(Transformers.AliasToBean<Course>()).List<Course>();
}
【问题讨论】:
-
你能告诉我们你试过的代码 sn-p 吗?到目前为止你有什么?有一些灵感:stackoverflow.com/questions/20528760 和文档:nhforge.org/doc/nh/en/index.html#queryqueryover
标签: nhibernate queryover