private void SavePhoneNumbers(User user, IList<PhoneNumber> phoneNumbers) { // Get ids of entities that allready exist in db(entities where id is greater than zero).New entities have zero for id. var idsOfNumsToKeep = phoneNumbers.Where(p => p.Id > 0).Select(p => p.Id).ToArray(); //Delete entities from db that are not in 'idsOfNumsToKeep'-array, because user //has deleted them var numsToDelete = context.PhoneNumber.Where(p => p.UserId == user.Id && !idsOfNumsToKeep.Contains(p.Id)); foreach(var num in numsToDelete) context.PhoneNumber.DeleteObject(num); foreach (var num in phoneNumbers) { num.UserId = User.Id; // An exsiting number update it if(num.Id > 0) { context.PhoneNumber.Attach(num) context.ObjectStateManager.ChangeObjectState(num, EntityState.Modified); } else{ // It's a new number add it context.PhoneNumber.AddObject(num); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)