1

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...

yougotiger
  • 434
  • 4
  • 18

3 Answers3

2

With built-in Profiles, no there isn't a better way. One option as provided by Tim is Table Profile provider or writing your own profile provider.

Or you can go completely other route i.e. storing profile information in your own custom table.

gbs
  • 7,196
  • 5
  • 43
  • 69
  • I've heard of the table profile provider that's mentioned, but our site will be small enough it isn't a huge deal. I was just wondering if there was a better way to do it.. Thanks. – yougotiger Mar 25 '11 at 18:19
0

You could probably do better with linq, I don't have VS with me right now, but pseudo code would look something like this:

var users = from MembershipUser user in Membership.GetAllUsers()
  where user.mysetting == desiredValue
  select user

then iterate over the users,

foreach(MembershipUser u in users) {
// do something
}

that should only contain the ones of interest. Linq should handle executing the SQL for you correctly, but you can check to see what it's doing with profiler.


EDIT

Actually that probably won't get you anything from a performance perspective, the GetAllUsers is going to bring back everything. You might want to create a linq2sql dbml map for the users table and use that instead of Membership class for querying against a custom property.


EDIT 2

ASP.NET Roles and Profiles: best way to query for collection of users who match custom profile property?

If you're using the table profile provider you may be able to use the linq query against that table: http://weblogs.asp.net/kencox/archive/2010/09/05/using-the-sql-table-profile-provider-in-asp-net-4-web-applications-c-amp-vb.aspx

Community
  • 1
  • 1
BlackICE
  • 8,816
  • 3
  • 53
  • 91
  • I guess I'm kind of confused, the MembershipUser object wouldn't contain anything from the profile so pulling it via Linq doesn't help wtih the profile information. – yougotiger Mar 25 '11 at 18:21
  • That's why I mentioned the table profile in Edit 2, you would want to write the linq query against that. – BlackICE Mar 25 '11 at 18:44
0

You could use the Table Profile Provider and build custom queries to get your desired settings.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939