Question

I'm trying to use Membership.GetAllUsers() to create a list of all of my users in the database. However, I have a model that I use that maps out the user properties (First Name, Last Name, Email, etc.).

How can I add all of the users into List<ManageUserViewModel>.

I've already tired:

List<ManageUserViewModel> model = Membership.GetAllUsers();

and then

MembershipUserCollection users = Membership.GetAllUsers();
List<ManageUserViewModel> model = new List<ManageUserViewModel>();

foreach (var item in users)
{
    model.Add(item);
}
Was it helpful?

Solution

If you're explicit with the object type in the foreach loop, you'll be able to access the user object you're looking for.

For example:

    var users = Membership.GetAllUsers();

    var userList = new List<MembershipUser>();
    foreach (MembershipUser user in users) 
    {
        userList.Add(user);
    }

OTHER TIPS

Membership.GetAllUsers() returns a MembershipUserCollection which in practice is a list of MembershipUser, whereas you want a list of ManageUserViewModel which I assume is an internal class to your application.

You can use LINQ for this:

var model = Membership.GetAllUsers()
              .Select(m => 
                    new ManageUserViewModel {/* set properties you need here */ }
              )
              .ToList();

Which is the equivalent of:

var users = Membership.GetAllUsers();
var model = new List<ManageUserViewModel>();

foreach (var item in users)
{
    model.Add(new ManageUserViewModel { /* set properties here */});
}

I had the same challenge. mine was with vb not c# and .NEt version 4.5 using visual studio 2013. I got all the solution from Microsoft website here best of luck

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top