【问题标题】:NHibernate 3.2 Linq with correlated subquery具有相关子查询的 NHibernate 3.2 Linq
【发布时间】:2011-06-02 21:44:26
【问题描述】:

任何人都可以帮助尝试在 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 进行等效操作。

【问题讨论】:

  • 你有什么运气吗?我正在尝试做几乎相同的事情。我已经弄清楚了 Linq 语句并使用了 LinqPad,但我无法让 NHibernate 执行它。见stackoverflow.com/questions/15206860/…

标签: linq nhibernate


【解决方案1】:

这里不需要相关的子查询。您的所有外部查询都是在Allow == true 时获取EntitySecurityKey.Name。您可以在查询后使用简单的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;
}

【讨论】:

    猜你喜欢
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多