开发者

Best way to check a setting in all profiles

开发者 https://www.devze.com 2023-02-19 08:55 出处:网络
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 t

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 th开发者_运维技巧at 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...


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消