NHibernate 3.2 Linq与相关子查询

编程入门 行业动态 更新时间:2024-10-26 06:26:52
本文介绍了NHibernate 3.2 Linq与相关子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人可以尝试在Linq到NHibernate 3.2中执行以下SQL吗?

Can anyone help with trying to do the following SQL in Linq to NHibernate 3.2?

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进行等效操作.

This works beautifully in SQL but I just cannot fathom how to do the equivalent using Linq.

推荐答案

此处无需相关子查询.您的所有外部查询要做的就是在Allow == true时获取EntitySecurityKey.Name.您可以在查询后使用简单的if语句执行该逻辑.

There is no need for a correlated sub-query here. All your outer query does is fetch EntitySecurityKey.Name when Allow == true. You can perform that logic with a simple if statement after your query.

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; }

更多推荐

NHibernate 3.2 Linq与相关子查询

本文发布于:2023-10-19 20:24:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508674.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NHibernate   Linq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!