Linq to SQL, return only users in a specific role

I am using linq for SQL with the asp.net membership provider. I have three roles, but you want to get only users of a certain role. This is my request to return all users

  public IQueryable<aspnet_User> GetAllMembers()
  {
     return db.aspnet_Users.OrderByDescending(u => u.UserName);
  }

Can someone point me in the right direction?

thank

+3
source share
3 answers

It sounds like you need the same thing discussed here . Tvanfosson's untested answer was the highest:

var users = db.aspnet_Users
              .Where(u => u.Active)
              .Any(u => u.aspnet_UsersInRoles
                         .Any(r => r.aspnet_Roles.RoleName == "Auth Level 1" ));

According to the person who asked the question (John), this did not quite work for him, so he did it instead:

var AuthUsers = from aspnet_User in db.aspnet_Users
                join userinroles in db.aspnet_UsersInRoles on aspnet_User.UserId equals userinroles.UserId
                where aspnet_User.Active 
                && userinroles.aspnet_Role.RoleName == "Auth Level 1"
                select aspnet_User;

, ( , ).

+2

, ?

...

0

- :

  • , aspnet_UsersInRole
  • aspnet_Users,

- :

string roleName = "someRole";

// get the user id in that role
var usersInRole = db.aspnet_UsersInRoles
                        .Where(ur => ur.RoleName == roleName)
                        .Select(ur => ur.UserId).ToList();

// then find the users that are in that list of users
var users = db.aspnet_Users.Where(u => usersInRole.Contains(u.UserId));

, , , , , , . , , .

, !: -)

0

Source: https://habr.com/ru/post/1726871/


All Articles