/// <summary> /// Updates the virtual machine list. /// </summary> /// <param name="isChecked">if set to <c>true</c> the checkbox is checked.</param> /// <param name="currentGroup">The current platform.</param> /// <param name="machineAssoc">The virtual machine/group association.</param> private void UpdateMachineList(bool isChecked, UserGroup currentGroup, VirtualMachineGroupAssoc machineAssoc) { // If the virtual machine isn't NULL, then update the virtual machine list to either // include or remove the virtual machine, depending on the check setting. if (machineAssoc != null) { var exists = currentGroup.VirtualMachineGroupAssocs.Any(x => x.GroupName.Equals(machineAssoc.GroupName) && x.MachineName.Equals(machineAssoc.MachineName)); if (isChecked && !exists) { // If this association has already been deleted (unchecked) without a save, then create a replacement entry // and add it back into the collection. Otherwise an exception will occur saying the object is in a deleted // state and can't be added. if (machineAssoc.EntityState == System.Data.EntityState.Deleted) { var replacement = VirtualMachineGroupAssoc.CreateVirtualMachineGroupAssoc(machineAssoc.MachineName, machineAssoc.GroupName); currentGroup.VirtualMachineGroupAssocs.Add(replacement); } else { currentGroup.VirtualMachineGroupAssocs.Add(machineAssoc); } } else if (!isChecked && exists) { var item = currentGroup.VirtualMachineGroupAssocs.First(x => x.GroupName.Equals(machineAssoc.GroupName) && x.MachineName.Equals(machineAssoc.MachineName)); currentGroup.VirtualMachineGroupAssocs.Remove(item); } } }
private VirtualMachineGroupAssoc GetMachineGroupAssoc(string groupName, string machineName) { VirtualMachineGroupAssoc result = (from a in _entities.VirtualMachineGroupAssocs where a.MachineName == machineName && a.GroupName == groupName select a).FirstOrDefault(); if (result == null) { result = new VirtualMachineGroupAssoc() { MachineName = machineName, GroupName = groupName }; } return(result); }