开发者

LINQ to Entities equivilent to SQL NOT IN

I am having problems converting the following SQL statement to LINQ to Entity.

SELECT     Name
FROM       Role
WHERE      ID NOT IN (SELECT Role_ID FROM UserRoleRelation WHERE User_ID = 11)

The three tables and their columns are

  • User (ID, Uesrname)
  • Role (ID, Name)
  • UserRoleRelation (ID, User_ID, Role_ID)

I tried the following

fro开发者_高级运维m r in db.Roles
where !db.UserRoleRelations.Any(p => p.User_ID == UserID)
select r

Any suggestions?

Mangesh was very close but it pointed me in the right direction.

this is the code that worked.

(from r in db.Roles
where !(from y in db.UserRoleRelations where y.User_ID == UserID select y.Role_ID).Contains(r.ID) 
select r);

Thanks for the help.


Here you go

   var result = (from r in db.Roles
                 where !(from y in db.UserRoleRelations
                         where y.User_Id == UserID
                         select y.User_ID).Contains(r.role)
                 select r.Name);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜