Search code examples
c#linqmembership-provider

Using Linq on MembershipUserCollection


I have this code which gives me a list of all the users by using my membership provider.

        var users = Membership.GetAllUsers();
        var usernames = new List<string>();
        foreach(MembershipUser m in users)
            usernames.Add(m.UserName);

I thought there should be an easier way to do this by using LINQ, but I can't seem to use LINQ on a MembershipUserCollection


Solution

  • Because MembershipUserCollection only implements IEnumerable, not IEnumerable<MembershipUser>, you need to use the Linq extension method Cast then then use other Linq extension methods:

    Membership.GetAllUsers ().Cast<MembershipUser> ().Select (m => m.UserName);