带有子查询的NHibernate查询

时间:2017-03-24 14:16:52

标签: c# nhibernate

我想写一个NHibernate查询,其结果是Employees列表,其中每个Employee都有一个名为isManager的属性。此属性的值取决于EmployeeRole表中是否有记录。

Employee结果类如下所示:

public class Employee {
    public long Id { get; set; }
    public bool IsManager { get; set; }
}

员工表:

Id int 名称nvarchar(250)

EmployeeRole表

Id int EmployeeId int RoleGuid uniqueidentifier 有效位

在常规SQL中,我会写一个这样的查询:

SELECT e.ID, e.Name,
    ((
        SELECT COUNT(Id) 
        FROM EmployeeRole r 
        WHERE r.EmployeeId = e.ID
        AND r.RoleGuid = '<ManagerRoleGuid>'
        AND r.Active = 1
    ) > 0) AS IsManager
FROM Employee

我该怎么做?

===================编辑=========================== ==

好的,我已经想到了这一点,一旦我改变了自己的思维方式,它实际上非常简单:

var query = session.QueryOver<Employee>(() => employeeAlias)
    .SelectList(list => list
        .Select(e => e.Id).WithAlias(() => employee.Id)
        .Select(e => e.Name).WithAlias(() => employee.Name)
        .SelectSubQuery(QueryOver.Of<EmployeeRole>()
            .Where(r => r.RoleGuid == '<ManagerRoleGuid>')
            .Where(r => r.Active == true).ToRowCountQuery()
    ).WithAlias(() => employee.IsManager))
.TransformUsing(Transformers.AliasToBean<Employee>());

0 个答案:

没有答案