2
我想爲查詢編寫分離標準。選擇列並在休眠中寫入案例
SELECT Id, sum(1) as total
,sum(CASE WHEN e.salary > 2000 THEN e.salary
ELSE 2000 END) "total Salary"
FROM employees e;
有人可以幫忙嗎?
我想爲查詢編寫分離標準。選擇列並在休眠中寫入案例
SELECT Id, sum(1) as total
,sum(CASE WHEN e.salary > 2000 THEN e.salary
ELSE 2000 END) "total Salary"
FROM employees e;
有人可以幫忙嗎?
我們可以這樣來做:
的一切,讓我們創造了條件首先,稍後計算:
var computed = Projections.Conditional(
Restrictions.Gt("Salary", 2000)
, Projections.Property("Salary")
, Projections.Constant(2000));
在這個時刻,我們有包裹computed
投影裏面的CASE語句。因此,讓我們使用它:
var session = ... // get the ISession
// criteria querying the Employee
var criteria = session.CreateCriteria<Employee>();
// the projections we need
criteria.SetProjection(
Projections.ProjectionList()
.Add(Projections.GroupProperty("Id"), "Id")
.Add(Projections.Sum(Projections.Constant(1)), "Total")
.Add(Projections.Sum(computed), "Salary")
);
// result transformer, converting the projections into EmployeeDTO
var list = criteria
.SetResultTransformer(Transformers.AliasToBean<EmployeeDTO>())
.List<EmployeeDTO>();
這可能是我們EmployeeDTO,如果薪水爲int:
public class EmployeeDTO
{
public virtual int ID { get; set; }
public virtual int Total { get; set; }
public virtual int Salary { get; set; }
}