Hello,
I have two observable collections: the first collection contains all devices and the seconds contains all devices after refresh!
I want to get the items on the second list that don't exist in the first list!
Hello,
I have two observable collections: the first collection contains all devices and the seconds contains all devices after refresh!
I want to get the items on the second list that don't exist in the first list!
Hello,
Thanks for answering
I found a solution to my problem
foreach (var newDevice in NewDevicesCollection)
{
if (DevicesCollection.Where(x => x.Id ==
newDevice.Id).Count()>0)
{
var device = DevicesCollection.Where(x => x.Id ==
newDevice.Id).First();
if (device.LastConnectionDateUtc !=
newDevice.LastConnectionDateUtc ||
device.State != newDevice.State||
)
{
int index = DevicesCollection.IndexOf(device);
DevicesCollection[index] = newDevice;
}
}
else
{
DevicesCollection.Add(newDevice);
}
}
Hello,
You could use .Except with and create a IEqualityComparer<T> Interface for the underlying class.
Given a Person class and you want to look for differences for FirstName and LastName
Comparer
public class PersonEqualityComparer : IEqualityComparer<Person>
{
public bool Equals(Person x, Person y)
{
if (x == y) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
if (x.GetType() != y.GetType()) return false;
return x.FirstName == y.FirstName && x.LastName == y.LastName;
}
public int GetHashCode(Person obj)
{
return HashCode.Combine(obj.FirstName, obj.LastName);
}
}
Person class
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString() => FirstName;
}
The following would show two differences
public class Examples
{
public static void Differences()
{
var persons1 = new ObservableCollection<Person>()
{
new() {Id = 1, FirstName = "Jon", LastName = "Smith"},
new() {Id = 2, FirstName = "Mary", LastName = "Smith"}
};
var persons2 = new ObservableCollection<Person>()
{
new() {Id = 1, FirstName = "Jon", LastName = "Smith"},
new() {Id = 2, FirstName = "Mary", LastName = "Smith"},
new() {Id = 3, FirstName = "Dan", LastName = "Smith"},
new() {Id = 4, FirstName = "Bill", LastName = "Smith"}
};
var result = persons2.ToList()
.Except(
persons1,
new PersonEqualityComparer()).ToList();
Debug.WriteLine(result.Count);
}
}

9 people are following this question.