如何比较两个`List 和List `?

如何将两个ListList ? 因此,如果Value具有不同的值,则应该可以检查它。

(我知道我们可以使用foreach ……但是我想要LINQ解决方案/)

谢谢!

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

 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.Default.Equals(x, y); } public int GetHashCode(object obj) { return EqualityComparer.Default.GetHashCode(obj); } } 

现在

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

你可以将两种方式等同起来:

 IStructuralEquatable equ = lst1 ; Console.WriteLine(equ.Equals(lst2 , EqualityComparer.Default)); or Console.WriteLine(equ.Equals(lst2 , new myComparer())); 

这个SO问题的接受答案建议使用Enumerable.SequenceEqual , 在此处记录 。

该方法需要两个IEnumerable和一个IEqualityComparer 。 它并行地遍历您的两个可枚举项,并使用您提供的比较器逐个元素地检查相等性。

在IEqualityComparer实现中,您可能希望通过Id属性比较MyObject实例。

如果确实有两种对象类型,你可以做一些像MyList1.Select(new MyObject2 {/*mapping goes here*/})或尝试使用AutoMapper

如果您不想(或不能)实现自己的IEqualityComparer ,则可以压缩和比较字段:

 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) 

你的类是一样的,所以我相信你想要做的是比较两个MyObject类型的对象列表:

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

我发现最简单的方法是在不必编写单独的IEqualityComparer ,让MyObject类实现IComparable接口。 这个在本页的中间详细解释,但这是您的类在实现界面后的样子:

 public class MyObject : IEquatable  { 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; } } 

一旦你的类有Equals()GetHashCode()方法,LINQ Except()方法将自动工作:

 List 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 objects2 = { new MyObject { Name = "apple", Value= "fruit", ID= 9 } }; List comparison = objects1.Except(objects2); 

comparison现在有橙子和柠檬,但不是苹果。 我喜欢这个解决方案,因为代码在最后一行是多么清晰。