/// <summary> /// Gets a collection of updated objects from the existing object collection and the /// added objects from the imported object collection in the same order that was /// found in the imported object collection. /// </summary> /// <returns>An ordered collection of updated and added elements.</returns> public IEnumerable <TTargetData> GetModifiedCollection() { KeyValuePair <int, TTargetData>[] remainingObjects = ObjectsToBeUpdated.Union(ObjectsToBeAdded).ToArray(); var orderedObjects = new TTargetData[remainingObjects.Length]; foreach (KeyValuePair <int, TTargetData> remainingObject in remainingObjects) { orderedObjects[remainingObject.Key] = remainingObject.Value; } return(orderedObjects); }
/// <summary> /// Creates a new instance of <see cref="CollectionModification"/>. /// </summary> /// <param name="existingObjects">The current collection of objects.</param> /// <param name="updatedObjects">The collection containing objects that were imported /// and thus could contain updates for the existing objects.</param> /// <param name="equalityComparer">The comparer to test whether elements in /// <paramref name="existingObjects"/> have a matching element in /// <paramref name="updatedObjects"/>.</param> /// <exception cref="ArgumentNullException">Thrown if any parameter is <c>null</c>.</exception> /// <exception cref="ArgumentException">Thrown when <paramref name="existingObjects"/> /// or <paramref name="updatedObjects"/> contains a <c>null</c> element.</exception> public CollectionModification(IEnumerable <TTargetData> existingObjects, IEnumerable <TTargetData> updatedObjects, IEqualityComparer <TTargetData> equalityComparer) { if (existingObjects == null) { throw new ArgumentNullException(nameof(existingObjects)); } if (updatedObjects == null) { throw new ArgumentNullException(nameof(updatedObjects)); } if (equalityComparer == null) { throw new ArgumentNullException(nameof(equalityComparer)); } if (existingObjects.Contains(null)) { throw new ArgumentException(@"Cannot determine modifications from a collection which contain null.", nameof(existingObjects)); } if (updatedObjects.Contains(null)) { throw new ArgumentException(@"Cannot determine modifications with a collection which contain null.", nameof(updatedObjects)); } TTargetData[] existingArray = existingObjects.ToArray(); var index = 0; foreach (TTargetData importedObject in updatedObjects) { int existingObjectIndex = FindIndex(existingArray, importedObject, equalityComparer); if (existingObjectIndex > -1) { ObjectsToBeUpdated.Add(index, existingArray[existingObjectIndex]); existingArray[existingObjectIndex] = null; } else { ObjectsToBeAdded.Add(index, importedObject); } index++; } ObjectsToBeRemoved = existingArray.Where(e => e != null); }
/// <summary> /// Finds out whether there was a difference between the existing and the imported /// object collections. /// </summary> /// <returns><c>true</c> if there were any updates, <c>false</c> otherwise.</returns> public bool HasUpdates() { return(ObjectsToBeRemoved.Any() || ObjectsToBeAdded.Any() || ObjectsToBeUpdated.Any()); }