【问题标题】:linq where clause not in select statementlinq where 子句不在 select 语句中
【发布时间】:2009-08-21 20:12:25
【问题描述】:

谁能帮我从 SQL Query 转换为 LINQ VB.NET:

select rls.* from Roles rls(nolock)
where rls.id not in (
select r.ID from usersRole ur (nolock)
inner join Roles r(nolock) on ur.RoleID = r.ID
where user_id = 'NY1772')

谢谢

【问题讨论】:

    标签: linq where-clause in-function


    【解决方案1】:

    我找到了自己的答案...

         'construct a where ID list
         Dim lstRoleIDs = From ur In ctx.UsersRoles _
                          Join rl In ctx.Roles _
                          On ur.RoleID Equals rl.ID _
                          Where ur.User_ID = UserId _
                          Select rl.ID
    
         Dim newQ = (From r In ctx.Roles _
                     Where Not lstRoleIDs.Contains( _
                            r.ID) _
                     Select New UserRoleList With {.ID = r.ID, .PermDesc = r.ID & " - " & r.Permission & " - " & r.PermissionDescription})
    

    【讨论】:

    • 恭喜。您发现处理棘手的 LINQ 查询的最佳方法之一是将其分解为组件部分,并允许提供者将表达式树重新组合在一起。您可能会发现将 lstRoleIDs 更改为更具描述性的内容(如 selectedUsersRoleIds)更易于维护。
    【解决方案2】:

    如果您想保留 (NOLOCK) 提示,我有 blogged a handy solution 在 C# 中使用扩展方法。请注意,这与向查询中的每个表添加 nolock 提示相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      相关资源
      最近更新 更多