问题
I have a membershipusercollection that I am trying to get one specific user from. Unfortunately I cannot user the MembershipUser.GetUser() method. So currently I have a collection of all membership users like so:
MembershipUserCollection mc = Membership.GetAllUsers();
What I would like to do is get one of these users by either login or email (doesn't matter which). I realize I can do a foreach loop on the collection and compare but I have to imagine there is a better way....
Thanks in advance...
For those curious why I cannot user the getuser method, lets just say it is because of sharepoint.
回答1:
Try this
mc.Cast<MembershipUser>().SingleOrDefault(m => m.UserName == "username");
回答2:
MembershipUser.GetUser() is not a valid method instead Membership.GetUser("johndoe").
var user = Membership.GetAllUsers()
.Cast<MembershipUser>()
.FirstOrDefault(m => m.Email == "john@doe.com" || m.UserName == "johndoe");
One thing you want to watch out is GetAllUsers retrieves all users from database.
回答3:
Apparently there is another way:
MembershipUser m = mc["username"];
来源:https://stackoverflow.com/questions/15411951/get-one-membershipuser-from-membershipusercollection