从另一个列表的列表创建选择列表

Create selectlist from list from another list

我有两个不同的模型。一个 UsersInRoles 模型和一个 Users 模型。用户可以担任多个角色,因此在 UsersInRoles 模型中,它将 userId 与 RoleId 相匹配。我需要做的是为特定角色的所有用户获取一个 selectList。我尝试了两种不同的方法,但都不起作用。

这是我尝试的第一种方法:

var techs = new HashSet<Guid>(db.usersInRoles.Where(u => u.RoleId == new Guid(Properties.Settings.Default.TechID)).Select(u => u.UserId));
ViewBag.SearchTechnician = new SelectList(db.Users.Where(u => u.UserId == techs.Contains<userId>).OrderBy(u => u.FirstName).ThenBy(u => u.LastName), "FullName", "FullName");

我在 .Contains 上收到错误消息:

The type or namespace name could not be found (are you missing a using directive or an assembly reference)?

我试过的第二种方法是:

ViewBag.SearchTechnician = new SelectList(from u in db.Users
                                          where u.UserId = (
                                          from ur in db.usersInRoles
                                          where ur.RoleId = new Guid(Properties.Settings.Default.TechID)
                                          select ur.UserId
                                          );

我在第二个 where 子句中收到一条错误消息,内容如下:

无法将类型 System.Guid 隐式转换为 bool

我走在正确的轨道上还是完全走错了路?

在您使用的第二种方法中使用 == 而不是 = 进行比较。

where ur.RoleId == new Guid(Properties.Settings.Default.TechID)