如何在nhibernate中使用case来按顺序使用子查询?

时间:2017-12-14 13:26:15

标签: sql sql-server nhibernate

我需要使用带有QueryOver的nhibernate从DB表中获取结果以进行下面的sql查询。 SQL请求是这样的:

SELECT Id, Name, Address,phone,ispassed  
FROM employee WHERE ispassed = 1   
ORDER BY   
(CASE WHEN id in (select empId from salary where empId in (2,45,65) and   Type=5) THEN 0 ELSE 1 END) asc,  
Name desc   

我陷入了秩序中。那么,请举例说明构建

的nhibernate查询
ORDER BY  
(CASE WHEN id in (select empId from salary where empId in (2,45,65)  
and Type=5) THEN 0 ELSE 1 END) asc

我有子查询:

var subquery = QueryOver.Of<salary>()
            .Where(x => x.Type == 5 && x.empId.IsIn(2,65,45))
            .SelectList(list => list
.Select(Projections.Distinct(Projections.Property<salary>(x => x.empId))));

当我在主查询中添加子查询时:

query.OrderBy(Projections.Conditional(
            Restrictions.In(Projections.SubQuery(subquery)),
            Projections.Constant(0),
            Projections.Constant(1))).Asc();

它显示错误&#34;最佳重载方法匹配&#39; NHibernate.Criterion.Projections.SubQuery(NHibernate.Criterion.DetachedCriteria)&#39;有一些无效的论点。&#34;

1 个答案:

答案 0 :(得分:0)

我希望你想要的是:

        Employee employeeAlias = null;
        var subquery = QueryOver.Of<salary>()
            .Where(x => x.Type == 5 && x.empId.IsIn(2,65,45))
            .And(x => x.empId == employeeAlias.Id) 
            .Select(x => x.empId);

        var query = session.QueryOver(() => alias)
                   .Where(x => x.IsPassed)
                   .OrderBy(Projections.Conditional(
                       Subqueries.Exists(subquery.DetachedCriteria),
                       Projections.Constant(0),
                       Projections.Constant(1))
                       ).Asc
                   .ThenBy(x => x.Name).Desc
                   //.Select
                   //.List 
                   ;