Reputation: 1234
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);
}
Upvotes: 2
Views: 10049
Reputation: 155
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
Upvotes: 0
Reputation: 320
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);
}
Upvotes: 6
Reputation: 31133
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 */});
}
Upvotes: 1