将此Linq查询从查询​​语法转换为lambda表达式

时间:2010-05-21 02:21:21

标签: linq lambda linq-query-syntax

我不确定我是否喜欢linq查询语法......这不是我的偏好。但我不知道这个查询使用lambda表达式会是什么样的,有人可以帮忙吗?

from securityRoles in user.SecurityRoles
from permissions in securityRoles.Permissions
where permissions.SecurableEntity.Name == "Unit" && permissions.PermissionType.Name == "Read"
orderby permissions.PermissionLevel.Value descending
select permissions

用户和安全角色之间存在多对多的关系,这使得这更加令人困惑。

谢谢! 凯利

2 个答案:

答案 0 :(得分:5)

像这样:

user.SecurityRoles.SelectMany(securityRoles => securityRoles.Permissions)
                  .Where(permissions => permissions.SecurableEntity.Name == "Unit" && permissions.PermissionType.Name == "Read")
                  .OrderByDescending(permissions => permissions.PermissionLevel.Value)

答案 1 :(得分:1)

        user.SecurityRoles.SelectMany(securityRoles => securityRoles.Permissions,
                                      (securityRoles, permissions) => new {securityRoles, permissions}).Where(
            @t => permissions.SecurableEntity.Name == "Unit" && permissions.PermissionType.Name == "Read").OrderByDescending(
            @t => permissions.PermissionLevel.Value).Select(@t => permissions)