Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4113144
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:17:10+00:00 2026-05-20T22:17:10+00:00

I wrote the following that does work, but want to know if there is

  • 0

I wrote the following that does work, but want to know if there is any more efficient ways to get all users without any role.

using System.Collections.Generic;
using System.Linq;
using System.Web.Security;

public static IEnumerable<MembershipUser> GetUsersHavingNoRole() {
  var allUsers = Membership.GetAllUsers().Cast<MembershipUser>();
  foreach (var user in allUsers) {
    if (Roles.GetRolesForUser(user.UserName).Length.Equals(0)) {
      yield return user;
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:17:10+00:00Added an answer on May 20, 2026 at 10:17 pm

    I’d expect there to usually be more users than roles – so it might make sense to iterate over the roles (return by Roles.GetAllRoles()), build up a list of users in any of those roles (e.g. by creating a HashSet<string> and adding the users for each role returned by Roles.GetUsersInRole), and then finding the difference between that and all the users. So if you’re using LINQ, you could use:

    var usersInRolesQuery = Roles.GetAllRoles()
                                 .SelectMany(role => Roles.GetUsersInRole(role));
    
    var usersInRoles = new HashSet<string>(usersInRolesQuery);
    return Membership.GetAllUsers()
                     .Cast<MembershipUser>()
                     .Where(user => !usersInRoles.Contains(user.UserName));
    

    Of course that’s still dealing with the same amount of data – but it may mean fewer roundtrips to whatever datastore is involved.

    Have you benchmarked the application to find out how expensive your current method is?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a simple program for work that does the following: read
The following code does not work correctly on Windows (but does on Linux): sock
I googled around, and I wrote the following code that I want to run
I want to do some performance measurement for a method that does some work
I'm trying to write a function that does the following: takes an array of
I wrote the following query to try and count the number of flights that
Is there a better to write the following? I have two services that I
For the following script, how can I write a function that returns all of
i wrote following code to create a linkbutton programmatically, but its showing like lable
I'm currently planning an application that does work on an object relational database. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.