Solved

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

Posted on 2010-08-16
2
625 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now