2
var usersFilterRoles = from u in dataContext.aspnet_Users
join p in dataContext.Partners on u.UserId equals p.Id
where u.aspnet_Roles.Contains(roles)

not worked. roles - I do RoleId or nameRole

I need get users by roles

Deniska d
  • 59
  • 11

3 Answers3

2

Get all user names using Roles.GetUsersInRole Method and If you need more information then use Membership.GetAllUsers() to get all users from the Membership and retrieve the Role users from the return MembershipUserCollection either using linq or loop through MembershipUserCollection and add to custom collection; which method you like best.

 MembershipUserCollection users;
  string[] usersInRole;


rolesArray = Roles.GetAllRoles();
users = Membership.GetAllUsers();

Check GetUsersIndRole method's link example to clear about the logic.

Expected Linq Query:

string[] usersInRole = Roles.GetUsersInRole("Role");
var users = DataContext.AspnetUsers.Where(usr => usersInRole.Contains(usr.LoweredUserName));

Follow these tutorials and Links for working with membership.
ASP.NET's Membership, Roles, and Profile
How do you manage asp.net SQL membership roles/users in production?

Roles.AddUsersToRole Method

Roles Class - Methods

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • No. I need query from database – Deniska d Dec 02 '11 at 06:29
  • If I need get "User" "Administrator" ? And I need join with Partners – Deniska d Dec 02 '11 at 07:43
  • BTW Mr.@Deniskad Nobody is here to do you job. everyone can give you idea and approach to get your target. so go to the GetUserInRole documentation at MSDN. There you will get all information .. and about your joining a user to another role etc.. i am updating some links in answer and .. change your approach to be depend on web. here you can get ideas not all your prepared work. – Niranjan Singh Dec 02 '11 at 07:50
  • check these links.. these will let you know about all expects that you are trying to do. well that was just suggestion.. happy to help.. – Niranjan Singh Dec 02 '11 at 08:26
0

Using the ASP.NET Role and Membership provider is the way to go but if you want to get the users by role in linq:

var userRoleQuery = from anu in context.AspNetUsers
                               where anu.AspNetRoles.Any(r => r.Id == roleId)
                               select anu.Id
carlo818
  • 209
  • 1
  • 3
  • 11
0

why not using System.Web.Security.Roles ?

using System.Web.Security;

namespace App.SomeName
{
    public class YourClass 
    {
         public void GetUsers(){
               string[] users = Roles.GetUsersInRole("MyRole"); 
         }
    }

}

Is this what you wanted to do ?

Paweł Staniec
  • 3,151
  • 3
  • 29
  • 41