与2 UserProfileValueCollection相比,更快的方法

本文关键字:方法 UserProfileValueCollection 相比 | 更新日期: 2023-09-27 18:10:49

User1的兴趣('Basketball','Hockey','Baseball')

User2的兴趣('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.
    }
}

我希望我可以使用一种更快的比较方式,因为我很有可能会比较200多个不同兴趣的用户。所以我最后会写这个

///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
    }
}

与2 UserProfileValueCollection相比,更快的方法

比较两个无序集合的通用方法(使用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);
}