question

AnasGuibene avatar image
0 Votes"
AnasGuibene asked AnasGuibene answered

How to get items of a collection that does not exist in another collection ?

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!

dotnet-csharpdotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AnasGuibene avatar image
0 Votes"
AnasGuibene answered

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);
                     }
                       
                 }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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);
     }
 }


93391-codedbykpmvp.png



codedbykpmvp.png (3.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.