使用where where where into join linq

时间:2012-03-29 19:38:06

标签: linq

我想只检索Rolepermissions,其中用户名是Bob但没有返回结果,我使用了这段代码:

var rolePermissions = from r in roles
                          join ur in userInRoles on r.RoleId equals ur.RoleId
                          join u in userModule.Where(x => x.UserName=="Bob" ) on ur.UserId equals u.UserId
                          join rp in rolePermission on r.RoleId equals rp.RoleId
select (new RolePermissions { RoleName= r.RoleName, Priority= rp.Priority, Modules= rp.Modules});

当我使用代码

时,我的数据库中有数据
var rolePermissions = from r in roles
                          join ur in userInRoles on r.RoleId equals ur.RoleId
                          join u in userModule on ur.UserId equals u.UserId
                          join rp in rolePermission on r.RoleId equals rp.RoleId
select (new RolePermissions { RoleName= r.RoleName, Priority= rp.Priority, Modules= rp.Modules});

它返回所有用户的权限,但如果我添加where子句返回any。

1 个答案:

答案 0 :(得分:1)

var rolePermissions = from r in roles
                      join ur in userInRoles on r.RoleId equals ur.RoleId
                      join u in userModule on ur.UserId equals u.UserId
                      join rp in rolePermission on r.RoleId equals rp.RoleId
                      where u.username = "Bob"
select (new RolePermissions { RoleName= r.RoleName, Priority= rp.Priority, Modules= rp.Modules});

更新where子句以使用存储的任何表用户名。或者使其成为传入的变量并使where语句使用变量。