I am removing the users from a user collection using linq as below. Is there a better of doing it? Can I merge the first and second query?
List<int> userIDs = ConfigurationManager.AppSettings["users"].Split(',').Select(userId => Convert.ToInt32(userid)).ToList();
foreach (int userId in userIDs)
{
userInfoList.RemoveAll(user => (user.UserId.Equals(userId)));
}
UserInfoList is a collection of users.
Thanks in advance
This would be more efficient:
If you have n elements in userIDs and m elements in userInfoList, then your solution is O(n * m). Using HashSet, the complexity becomes O(n + m) – much better. (Assuming the hash table operations are constant time).