Return differences between two listings

I am trying to distinguish between the two collections.

private ObservableCollection<SomeObject> _objectList = null; private ObservableCollection<SomeObject> _cachedObjectList = null; 

SomeObject implements IEquatable<SomeObject>

I use the following to determine if I have two collections:

 this._objectList.ToList().OrderBy(x => x.Id).SequenceEqual(this._cachedObjectList.ToList().OrderBy(x => x.Id)); 
  • The _cachedObjectList collection will not change.
  • You can add, delete or modify an object in the _objectList collection.

How can I return a new list containing a new added, deleted or modified object from two collections.

Any help would be greatly appreciated!

Implementation of IEquatable for SomeObject:

 public class SomeObject : IEquatable<SomeObject> { public int GetHashCode(SomeObject object) { return base.GetHashCode(); } public bool Equals(SomeObject other) { bool result = true; if (Object.ReferenceEquals(other, null)) { result = false; } //Check whether the compared objects reference the same data. if (Object.ReferenceEquals(this, other)) { result = true; } else { // if the reference isn't the same, we can check the properties for equality if (!this.Id.Equals(other.Id)) { result = false; } if (!this.OtherList.OrderBy(x => x.Id).ToList().SequenceEqual(other.OtherList.OrderBy(x => x.Id).ToList())) { result = false; } } return result; } } } 

EDIT: I need changes only if _objectList contains a modified object based on IEquatable.Equals (), after which I would like to return it. Otherwise, return new objects or deleted objects to the list.

+6
source share
1 answer

You will find the differences with

 var newAndChanged = _objectList.Except(_cachedObjectList); var removedAndChanged = _cachedObjectList.Except(_objectList); var changed = newAndChanged.Concat(removedAndChanged); 
+7
source

Source: https://habr.com/ru/post/900739/


All Articles