ADLib.ADUser.Sync_Groups C# (CSharp) Method

Sync_Groups() private method

Synchronize groups between the local ADUser object and the AD server record
private Sync_Groups ( ) : void
return void
        private void Sync_Groups()
        {
            //Sync groups
            HashSet<string> oldGroups = new HashSet<string>(_sourceUser.GetGroups(_connection.GlobalContext).Select(item => item.Name));
            HashSet<string> newGroups = new HashSet<string>(this.Groups);

            //Add new groups
            foreach (String groupName in newGroups.Where(item => !oldGroups.Contains(item)))
            {
                addToGroup(groupName);
            }

            if (newGroups.Count > 0)
            {
                //Remove missing groups
                foreach (String groupName in oldGroups.Where(item => !newGroups.Contains(item)))
                {
                    removeFromGroup(groupName);
                }
            }
            _groups = new List<string>(_sourceUser.GetGroups(_connection.GlobalContext).Select(item => item.Name));
        }