views:

476

answers:

2

Can someone help me to convert from SQL Query to 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')

Thanks

+2  A: 

i find my own answer...

     '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})
Annie
Congratulations. You found one of the best ways of working through a tricky LINQ query is to break it up into the component parts and allow the provider to piece the expression tree back together again. You might find it more maintainable to change lstRoleIDs to something more descriptive like selectedUsersRoleIds.
Jim Wooley
A: 

If you want to preserve the (NOLOCK) hints, I have blogged a handy solution using extension methods in C#. Note that this is the same as adding nolock hints to every table in the query.

RyanHennig