I'm wondering how I could optimize this method that takes in a list of UserPrincipal objects, filters them by Regex and inserts the new class instance into a list. It takes 4 to 5 seconds for an input list of 2000 entries, which seems too slow for that amount of entries.
private static Dictionary<string, User> ProcessUsers(IEnumerable<UserPrincipal> users) {
Dictionary<string, User> toReturn= new Dictionary<string, User>();
Regex regex = new Regex(@"^[a-zA-Z]{3}(\d{4})$");
foreach (UserPrincipal user in users)
{
if (regex.IsMatch(user.SamAccountName))
{
User newUser = new User(user.Name, user.SamAccountName.ToUpper(), user.EmailAddress, user);
toReturn.TryAdd(newUser.ID, newUser);
}
}
return toReturn;
}
I've tried Parallel.ForEach, but it didn't help at all.
EDIT: User class properties and constructor:
internal class User
{
public string name { get; set; }
public string ID { get; set; }
public string email { get; set; }
public UserPrincipal? activeDirectoryHandle { get; set; }
public User(string inName = "None", string inID = "None", string inEmail = "None", UserPrincipal? adHandle=null)
{
name = inName;
ID = inID;
email = inEmail;
activeDirectoryHandle = adHandle;
}
}
User
class? \$\endgroup\$users
collection contains multipleUserPrincipal
s where theSamAccountName
is the same? \$\endgroup\$RegexOptions.Compiled
? \$\endgroup\$