How to compare two `List <MyObject1> with a list <MyObject2>`?

How to compare two List<MyObject1> with List<MyObject2> ? Therefore, if the value of Value has a different value, it can be checked.

(I know that we can use foreach ... But I would like a LINQ solution /)

Thanks!

 public sealed class MyObject1 { public string Name { get; set; } public string Value { set; get; } public Guid ID { get; set; } } public sealed class MyObject2 { public string Name { get; set; } public string Value { set; get; } public Guid ID { get; set; } } 
+4
source share
4 answers
 public class myComparer: IEqualityComparer { public new bool Equals(object x, object y) { if (x is string ) return x == y; else if (x is Guid ) // maybe you want them to be equal if last char is equal. so you can do it here. return x == y; else return EqualityComparer<object>.Default.Equals(x, y); } public int GetHashCode(object obj) { return EqualityComparer<object>.Default.GetHashCode(obj); } } 

Now

 List<MyObject1 > lst1 = new MyObject1 <MyObject1 >(); //add items here... List<MyObject2 > lst2 = new MyObject1 <MyObject2 >(); //add items here... 

you can equate 2 ways:

 IStructuralEquatable equ = lst1 ; Console.WriteLine(equ.Equals(lst2 , EqualityComparer<object>.Default)); or Console.WriteLine(equ.Equals(lst2 , new myComparer())); 
+2
source

The accepted answer to this question is SO suggests using the Enumerable.SequenceEqual documented here .

The method accepts two IEnumerable and IEqualityComparer . It iterates over both lists in parallel and checks the element of equality by element using the comparator you provided.

In the implementation of IEqualityComparer, you can compare instances of MyObject against your Id properties.

If there really are two types of objects, you can do something like MyList1.Select(new MyObject2 {/*mapping goes here*/}) or try using AutoMapper

+7
source

If you do not want (or cannot) implement your own IEqualityComparer , you can archive and compare the fields:

 list1.Count == list2.Count && list1 .Zip(list2, (i1,i2) => new{i1,i2}) .All(x => x.i1.Name == x.i2.Name && x.i1.Value == x.i2.Value && x.i1.ID == x.i2.ID) 
+2
source

Your classes are the same, so I believe you want to compare two lists of objects of type MyObject :

 public class MyObject { public string Name { get; set; } public string Value { set; get; } public Guid ID { get; set; } } 

I find the easiest way to do this without writing a separate IEqualityComparer is to have a MyObject class that implements the IComparable interface. This is explained in detail halfway down this page , but it will look like your class after implementing the interface:

 public class MyObject : IEquatable <MyObject > { public string Name { get; set; } public string Value { set; get; } public Guid ID { get; set; } public bool Equals(MyObject other) { //Check whether the compared object is null. if (Object.ReferenceEquals(other, null)) return false; //Check whether the compared object references the same data. if (Object.ReferenceEquals(this, other)) return true; //Check whether the objects properties are equal. return Name.Equals(other.Name) && Value.Equals(other.Value) && ID.Equals(other.ID); } public override int GetHashCode() { //Get hash code for the Name field if it is not null. int hashName = Name == null ? 0 : Name.GetHashCode(); //Get hash code for the Value field. int hashCode = Value == null ? 0 : Value .GetHashCode(); //Get hash code for the IDfield. int hashID = ID.GetHashCode(); //Calculate the hash code for the entire object. return hashName ^ hashCode ^ hashId; } } 

As soon as your class has Equals() and GetHashCode() methods, the LINQ Except() method will automatically work:

 List<MyObject> objects1 = { new MyObject{ Name = "apple", Value= "fruit", ID= 9 }, new MyObject{ Name = "orange", Value= "fruit", ID= 4 }, new MyObject{ Name = "lemon", Value= "fruit", ID= 12 } }; List<MyObject> objects2 = { new MyObject { Name = "apple", Value= "fruit", ID= 9 } }; List<MyObject> comparison = objects1.Except(objects2); 

comparison now there is an orange and a lemon, but not an apple. I like this solution because of how readable the code is on the last line.

+1
source

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


All Articles