Friends, the task is to compare two lists of objects and display the difference in the fields in which it is.

The problem is that one of the fields is a nested collection.

For example, there is an XMLModel class and a collection with XMLPeriod:

public class XMLModel { public string Famaliya { internal get; set; } public string Imya { get; set; } public string Otchestvo { get; set; } public string Snlis { get; set; } public List<XMLPeriod> Periods { get; set; } } public class XMLPeriod { public string StartPeriod { get; set; } public string EndPeriod { get; set; } public string Lgota { get; set; } public string TU { get; set; } public string OUT { get; set; } public string PostitionList { get; set; } public string Dekret { get; set; } public string Visluga { get; set; } public string VislugaStavka { get; set; } } 

I honestly do not even know what side to approach. Implement the comparison of objects in the first approximation, you can probably through IEquatable so:

 public class XMLModel : IEquatable<XMLModel> { public string Famaliya { internal get; set; } public string Imya { get; set; } public string Otchestvo { get; set; } public string Snlis { get; set; } public List<XMLPeriod> Periods { get; set; } public bool Equals(XMLModel other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return string.Equals(Famaliya, other.Famaliya, StringComparison.OrdinalIgnoreCase) && string.Equals(Imya, other.Imya, StringComparison.OrdinalIgnoreCase) && string.Equals(Otchestvo, other.Otchestvo, StringComparison.OrdinalIgnoreCase) && string.Equals(Snlis, other.Snlis, StringComparison.OrdinalIgnoreCase) && !Periods.Except(other.Periods).Any(); ; } public override int GetHashCode() { unchecked { var hashCode = (Famaliya != null ? Famaliya.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Imya != null ? Imya.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Otchestvo != null ? Otchestvo.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Snlis != null ? Snlis.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Periods.Distinct().Aggregate(0, (x, y) => x.GetHashCode() ^ y.GetHashCode())); return hashCode; } } } public class XMLPeriod : IEquatable<XMLPeriod> { public string StartPeriod { get; set; } public string EndPeriod { get; set; } public string Lgota { get; set; } public string TU { get; set; } public string OUT { get; set; } public string PostitionList { get; set; } public string Dekret { get; set; } public string Visluga { get; set; } public string VislugaStavka { get; set; } public bool Equals(XMLPeriod other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return string.Equals(StartPeriod, other.StartPeriod, StringComparison.OrdinalIgnoreCase) && string.Equals(EndPeriod, other.EndPeriod, StringComparison.OrdinalIgnoreCase) && string.Equals(Lgota, other.Lgota, StringComparison.OrdinalIgnoreCase) && string.Equals(TU, other.TU, StringComparison.OrdinalIgnoreCase) && string.Equals(OUT, other.OUT, StringComparison.OrdinalIgnoreCase) && string.Equals(PostitionList, other.PostitionList, StringComparison.OrdinalIgnoreCase) && string.Equals(Dekret, other.Dekret, StringComparison.OrdinalIgnoreCase) && string.Equals(Visluga, other.Visluga, StringComparison.OrdinalIgnoreCase) && string.Equals(VislugaStavka, other.VislugaStavka, StringComparison.OrdinalIgnoreCase); } public override int GetHashCode() { unchecked { var hashCode = (StartPeriod != null ? StartPeriod.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (EndPeriod != null ? EndPeriod.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Lgota != null ? Lgota.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (TU != null ? TU.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (OUT != null ? OUT.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (PostitionList != null ? PostitionList.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Dekret != null ? Dekret.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Visluga != null ? Visluga.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (VislugaStavka != null ? VislugaStavka.GetHashCode() : 0); return hashCode; } } } 

In order to understand how to compare two XMLModel objects, I compare 4 fields, implement the IEquatable interface in XMLPeriod, and check the contents of the lists in Equals of the XMLModel! Periods.Except (other.Periods) .Any ();

All this will only give the fact that they are different. How to go further, and in the case of the difference, to find it and display it?

  • Do you get that Pupkin A.A. with two periods and Pupkin A.A. with three periods - different objects? - RusArt
  • You can simply compare according to SNILS - RusArt
  • @Ruslan Artamonov, that's right. But I want to see what is the difference. Because Equals, can count different, even if two periods. For example, different values ​​of the fields PostitionList. I want to see this difference - Evgeny Mitrokhin

1 answer 1

Can be implemented via Reflection

 public class XMLPeriod { public string StartPeriod { get; set; } public string EndPeriod { get; set; } public string Lgota { get; set; } public string TU { get; set; } public string OUT { get; set; } public string PostitionList { get; set; } public string Dekret { get; set; } public string Visluga { get; set; } public string VislugaStavka { get; set; } // получение свойств, которые не совпадают public List<PropertyInfo> GetDifference(XMLPeriod other) { return typeof (XMLPeriod).GetProperties().Where(x => !x.GetValue(this).Equals(x.GetValue(other))).ToList(); } } 

Adjust further as you need, because it is not clear how you want to compare the sheet with the sheet. All with all, or in order, or to search for the minimum difference, or to compare those for which some field coincides. You need to think more.