任何人都可以帮助尝试在 Linq to NHibernate 3.2 中执行以下 SQL 吗?
select act.Name from Activity act
where 1 =
(
select top 1 p.Allow
from Permissions p inner join Operations o on p.OperationId = o.OperationId
inner join Users u on p.UserId = u.UserId
where p.EntitySecurityKey = act.ActivityId and o.Name = '/operation'
and u.Name = 'user'
order by p.Level desc, p.Allow asc
)
这在 SQL 中工作得很好,但我就是无法理解如何使用 Linq 来完成相同的操作。
这里不需要相关子查询。您的外部查询所做的只是获取EntitySecurityKey.Name
when Allow == true
。您可以使用简单的方法来执行该逻辑if
查询后的声明。
private string GetEntitySecurityKeyNameIfAllowed(ISession session, string operationName, string userName)
{
var result = session.Query<Permission>()
.Where(p => p.Operation.Name == operationName
&& p.User.Name == userName)
.OrderByDescending(p => p.Level)
.ThenBy(p => p.Allow)
.Select(p => new
{
p.Allow,
p.EntitySecurityKey.Name
})
.FirstOrDefault();
return result != null && result.Allow
? result.Name
: null;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)