Question

Using ASP.net, is there a recommended best way to access a particular field of the profile in code. I was looking in my old Problem-Design-Solution 2.0 book, and it does it by pulling all members in the DB and then iterating through each one's profile (see code below). Is there a better way?

for each (MembershipUser user in Membership.GetAllUsers())
{
    ProfileCommon userProfile = profile.GetProfile(user.UserName);
    if (userProfile.mysetting == desiredValue)
    { 
         //do something
    }
}

Edit 1

I found that it can be done a little more efficiently than pulling members and then pulling profiles. It is possible that not all members have a profile, so if you use the following code, you'll pull all the profiles (which may be fewer in number than members, and then can iterate across it:

for each (ProfileInfo theProfile in ProfileManager.GetAllProfiles (ProfileAuthenticationOption.All)
{
    ProfileCommon pc = ProfileBase.Create(theProfile.UserName)
    if (pc.mysetting == desiredValue)
    {
        //do something
    }
}

It still round trips the DB for each profile, but it may not do it as many as if we used the members...

No correct solution

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