NHibernate查询在单个表中查找具有相等列和一些不同列的记录

时间:2011-12-26 16:17:15

标签: nhibernate fluent-nhibernate

尝试编写流畅的查询以完成以下操作:查找为同一学生提供的服务,在同一天但使用不同的代码。 sql查询看起来像:

select * from [Service] t1
where exists (
select 
    * 
from [Service] t2 
where 
t1.StudentId = t2.StudentId and
t1.ServiceDate = t2.ServiceDate and 
t1.ProcedureCodeId <> t2.ProcedureCodeId )

1 个答案:

答案 0 :(得分:0)

注意:代码来自没有编译器的内存,必须进行调整

Service serviceAlias = null;

var subquery = QueryOver.Of<Service>()
    .WhereRestrictonOn(s => s.Student.Id).EqProperty(() => serviceAlias.Student.Id)
    .WhereRestrictonOn(s => s.Date).EqProperty(() => serviceAlias.Date)
    .WhereRestrictonOn(s => s.ProcedureCode.Id).NotEqProperty(() => serviceAlias.ProcedureCode.Id);

var results = session.QueryOver(() => serviceAlias)
    .WithSubqueries.WhereExists(subquery)
    .List();