在Asp-Identity-core中获取具有特定角色的应用程序用户

时间:2017-01-11 18:25:58

标签: asp.net-core-identity

我希望获得一个属于类似角色的linq用户列表;像这样的东西:

{
   {
       string i_want_to_delete_this = "I don't want this cleared, but            
completely gone";
   }     // it's gone
}

1 个答案:

答案 0 :(得分:0)

首先创建查询以查找相关的userId然后使用此查询来获取用户列表。因为第一个查询尚未执行,所以最后只会在此处发出一个SQL查询:

public IList<User> GetApplicationUsersInRole(string roleName)
{
  var roleUserIdsQuery = from role in _context.Roles
                         where role.Name == roleName
                         from user in role.Users
                         select user.UserId;
  return _context.Users.Where(applicationUser => 
                                 roleUserIdsQuery.Contains(applicationUser.Id))
                         .ToList();
}