问题描述
我被编码的一个示例应用程序学习ASP.Net MVC3 EF4。我使用的是标准的成员资格提供程序。我有requiresUniqueEmail =真,每这样的MembershipUser不超过1电子邮件成员资格人员。
我要找到他们的电子邮件地址的特定用户。有一种功能,可为:
MembershipUserCollection用户= Membership.FindUsersByEmail(model.Email);
因为我有独特的电子邮件,我怎么从MembershipUserCollection中的MembershipUser?我知道我可以做一个foreach循环:
的foreach(在用户的用户的MembershipUser)
{
用户名= user.UserName;
}
但有刚刚访问的第一行?
一个更快的方法这不起作用:用户的MembershipUser用户= [0];
但是这是什么样的我要找的。 P>
谢谢!
更新:这工作,但我仍然有兴趣在回答上面的:
字符串USERNAME1 = Membership.GetUserNameByEmail(model.Email);
用户的MembershipUser = Membership.GetUser(USERNAME1);
因为你必须为每个用户为什么要使用独特的邮件 MembershipUserCollection
类
而您可以通过直接得到它。
字符串username = Membership.GetUserNameByEmail(model.Email); OMU的MembershipUser = Membership.GetUser(用户名);
雄武
对象会给你你在通过电子邮件地址GetUserNameByEmail(model.Email)用户的所有属性
根据问题的更新修改
我认为这是无关紧要的装载一个集合,你只有一个记录,内存消耗会更多比字符串的用户名(这是在我的例子中),再加上你在寻找一个记录的问题(如你是利用获取的信息循环,造成不必要的延误)。此外code线被降低(编译code变得更加容易和更快)。
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.)
这篇关于得到MembershipUserCollection单的MembershipUser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!