简体   繁体   中英

faster way to compare to 2 UserProfileValueCollection

User1's Interest ('Basketball','Hockey','Baseball')

User2's Interest ('Boxing','Basketball')

using(SPSite site = new SPSite(SPContext.Current.Web.Url)){
    using(SPWeb web = site.OpenWeb()){
        ServerContext oContext = ServerContext.GetContext(site);
        UserProfileManager upManager = new UserProfileManager(oContext); 
        UserProfile User1Profile = upManager.GetUserProfile(user1.LoginName); // user1 is SPUser
        UserProfile User2Profile = upManager.GetUserProfile(user2.LoginName); // user2 is SPUser

        /// Faster way to check if the interest of User1 have something common in User2
        /// In the interest list above user1 and user2 have a common interest on Basketball
        /// How will I do this checking. I prefer a faster approach like the  Array.IndexOf
        /// but this can't be done on the UserProfileValueCollection.

    }
}

I'm hoping that I could use a faster way of comparison because there is a big chance that I will be comparing more than 200 users with different interest. So I would end up doing this

///Search common interest among the members of the group
foreach(SPUser user in oweb.Groups[0].Users){
    if(CurrentlyLoggedInUser have common interest with user){
        ///do the necessary logic here
    }
}

A generic way of comparing two unordered collections (using Linq):

bool CompareStringCollections(IEnumerable<string> a, IEnumerable<string> b) 
{
   // If you know they implement the Count instead of Count() 
   // use then the right type
   if ( a.Count() != b.Count() )
       return false;
   var hs = new HashSet(a);
   return b.All(hs.Contains);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM