Solved

How can i call Membership.GetAllUsers but only get users within a specified role

Posted on 2010-08-16
2
628 Views
Last Modified: 2012-05-10
How can i call Membership.GetAllUsers but only get users within a specified role.

I'm aware of Roles.GetUsersInRole("Comgem") however this doesnt return the membersip columns i'm looking for.

Is this possible, while maintaining the same results set returned by GetAllusers?

Thanks
0
Comment
Question by:Webbo_1980
  • 2
2 Comments
 
LVL 22

Expert Comment

by:Snarf0001
ID: 33450164
No single step way to do it:
The MembershipUser itself has no knowledge of what roles a user belongs to, that has to be queried from the RoleProvider as you've shown.

But, if you can use linq, a pretty easy to query the objects to get what you're after:
var members = from m in Membership.GetAllUsers().OfType<MembershipUser>()
                join u in Roles.GetUsersInRole("Comgem") on m.UserName equals u
                select m;

Open in new window

0
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 33450195
Note though, that depending on what provider you're using, and how many users you have vs how many are in the target role, there's a really good chance you'd get better performance by calling GetUsersInRole by itself, and then for each one returning the Membership.GetUser("").

var members = Roles.GetUsersInRole("Comgem").Select(u => Membership.GetUser(u));
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question