I'm learning ASP.Net MVC3 EF4 by coding a sample app. I'm using the standard Membership Provider. I have the requiresUniqueEmail="true" in the Membership provider so no more than 1 email per MembershipUser.
I want to find a particular user by their email address. There is a function that helps:
MembershipUserCollection users = Membership.FindUsersByEmail(model.Email);
Since I have unique emails, how do I get the MembershipUser from the MembershipUserCollection? I know I can do a foreach loop:
foreach (MembershipUser user in users)
{
username = user.UserName;
}
but is there a quicker way of just accessing the first row?
This doesn't work: MembershipUser user = users[0];
but that is kind of what I'm looking for.
Thanks!!
UPDATE: This works but I'm still interested in an answer to the above:
string username1 = Membership.GetUserNameByEmail(model.Email);
MembershipUser user = Membership.GetUser(username1);
since you have unique email for each users why are you using MembershipUserCollection
Class
rather you can directly get it using
string userName = Membership.GetUserNameByEmail(model.Email);
MembershipUser oMu = Membership.GetUser(userName);
oMu
object will give you all the properties of the user with email address you have passed in GetUserNameByEmail(model.Email)
EDIT based on update in question
I think it is irrelevant to load a collection where you have only single record, memory consumption will be more as compared to string username (which is used in my example), plus you have problems in finding the single record (where you are using loop, unnecessary delay in fetching the details). Additionally line of code is reduced (compiling the code become more easy and faster.)