public bool Equals(Group objectToCompare) { if (objectToCompare == null) { return(false); } if (ReferenceEquals(this, objectToCompare)) { return(true); } if (UserIds.Count != objectToCompare.UserIds.Count || CreateByUserId != objectToCompare.CreateByUserId) { return(false); } //If you need equality when order matters - use this //return UserIds.SequenceEqual(objectToCompare.UserIds); //This is for set equality. If this is your case and you don't allow duplicates then I would suggest to use HashSet<int> or ISet<int> instead of Collection<int> //and use their methods for more concise and effective comparison return(UserIds.All(id => objectToCompare.UserIds.Contains(id)) && objectToCompare.UserIds.All(id => UserIds.Contains(id))); }
/// <summary> /// Дополнительные изменения в БД после обновления данных сущности в БД. /// </summary> /// <param name="db">контекст БД</param> /// <param name="entity">обновляемая в БД сущность</param> public void AfterUpdate(ApplicationDBContext db, Room entity) { if (UserIds == null) { return; } if (entity.Users == null) { entity.Users = new List <User>(); } var userIdsToBeAdded = UserIds; var usersToBeDeleted = entity.Users .Where(us => UserIds.All(usId => usId != us.Id)).ToList(); foreach (var user in usersToBeDeleted) { entity.Users.Remove(user); } userIdsToBeAdded = UserIds .Where(usId => entity.Users.All(us => us.Id != usId)).ToList(); foreach (var userId in userIdsToBeAdded) { entity.Users.Add(db.Set <User>().Find(userId)); } }