我有一个SQL查询
select distinct e.ID, e.FIRST_NAME, e.LAST_NAME, ef.PROJECT, r.NAME
from EMPLOYEE as e
inner join [RANK] as r on r.ID = e.[RANK]
inner join [EMPLOYEE_FUNCTION_IN_PROJECT] as ef on e.ID = ef.EMPLOYEE
where ef.PROJECT
in (
select ef.PROJECT
from [EMPLOYEE_FUNCTION_IN_PROJECT] as ef
group by ef.PROJECT
having COUNT(distinct ef.EMPLOYEE) >= 3
)
and ef.[FUNCTION] = 1
现在我想转换为NHibernate查询。这就是我到目前为止所做的事情
using (ITransaction myTransaction = mySession.BeginTransaction())
{
EmployeeDAO empDaoAlias = null;
Employee empAlias = null;
Group groupAlias = null;
Project projectAlias = null;
Rank rankAlias = null;
EmployeeFunctionInProject efpAlias = null;
var subquery = QueryOver.Of<EmployeeFunctionInProject>(() => efpAlias)
.Select(Projections.GroupProperty(
Projections.Property<EmployeeFunctionInProject>(e => e.Project)))
.Where(Restrictions.Gt(Projections.Count<EmployeeFunctionInProject>(e => e.Employee), 3));
empList = mySession.QueryOver<Employee>(() => empAlias)
.Inner.JoinAlias(() => empAlias.Rank, () => rankAlias)
//.Where(Restrictions.Eq(Projections.Property<Rank>(r => r.Id), 1)) //indicate this is group leader
.WithSubquery
.WhereProperty(() => empAlias.Id)
.In(subquery)
.Select(Projections.ProjectionList()
.Add(Projections.Property(() => empAlias.Id).WithAlias(() => empDaoAlias.Id))
.Add(Projections.Property(() => empAlias.FirstName).WithAlias(() => empDaoAlias.FirstName))
.Add(Projections.Property(() => empAlias.LastName).WithAlias(() => empDaoAlias.LastName))
.Add(Projections.Property(() => ??).WithAlias(() => empDaoAlias.Project))
.Add(Projections.Property(() => rankAlias.Name).WithAlias(() => empDaoAlias.Rank)))
.TransformUsing(Transformers.AliasToBean<EmployeeDAO>())
.List<EmployeeDAO>().ToList();
}
首先,我需要修改
.Add(Projections.Property(() => ??).WithAlias(() => empDaoAlias.Project))
显示项目ID
其次,查询似乎无法正常工作,有人可以帮我纠正吗?
提前致谢!!!
答案 0 :(得分:0)
您想要实现的目标更容易:
但是,让我们尝试以另一种方式解决问题:如果您不打算更改基础数据库,请使用EF进行查询/报告。 CQRS 是一种常用的模式。建议使用EF进行查询(NH更新)在我们过去4 - 6年实现的项目中非常常见。