Мне нужно сравнить два списка типа Slide, которые содержат другой Список элементов, называемых Charts. я должен найти все различия между слайд-листами, причем разница может быть:Сравните два списка и получите все различия?
слайда в списке А, но не в B
слайд в списке А, но не в B
a Слайд, который находится в обоих списках, но их Диаграммы разные
Я попытался использовать исключение, но он возвращает списки с разными диаграммами как «то же самое». Пример Список A: A B C D Список B: A B C D * (d содержит разные графики) Должен возвращать D *, но это не сработает.
Я немного смущен, почему это происходит - для сравнения выглядит нормально для меня.
Мой код:
class PPDetectDifferences
{
private PPPresentation laterVersion;
private string Path { get; set; }
private PPPresentation OriginalPresentation { get; set; }
private PPPresentation GetLaterPresentation()
{
var ppDal = new PPDAL(Path);
Task<PPPresentation> task = Task.Run(() => ppDal.GetPresentation());
var presentation = task.Result;
return presentation;
}
public PPDetectDifferences(string path, PPPresentation ppPresentation)
{
if (path != null)
{
this.Path = path;
}
else
{
throw new ArgumentNullException("path");
}
if (ppPresentation != null)
{
this.OriginalPresentation = ppPresentation;
}
else
{
throw new ArgumentNullException("ppPresentation");
}
}
public bool IsDifferent()
{
//// getting the new List of Slides
laterVersion = GetLaterPresentation();
//// Compare the newer version with the older version
var result = laterVersion.Slides.Except(OriginalPresentation.Slides, new PPSlideComparer()).ToList();
//// If there are no differences, result.count should be 0, otherwise some other value.
return result.Count != 0;
}
}
/// <summary>
/// Compares two Slides with each other
/// </summary>
public class PPSlideComparer : IEqualityComparer<PPSlide>
{
public int GetHashCode(PPSlide slide)
{
if (slide == null)
{
return 0;
}
//// ID is an INT, which is unique to this Slide
return slide.ID.GetHashCode();
}
public bool Equals(PPSlide s1, PPSlide s2)
{
var s1Charts = (from x in s1.Charts select x).ToList();
var s2Charts = (from x in s2.Charts select x).ToList();
var result = s1Charts.Except(s2Charts, new PPChartComparer()).ToList();
return result.Count == 0;
}
}
/// <summary>
/// Compares two Charts with each other
/// </summary>
public class PPChartComparer : IEqualityComparer<PPChart>
{
public int GetHashCode(PPChart chart)
{
//// UID is an INT, which is unique to this chart
return chart == null ? 0 : chart.UID.GetHashCode();
}
public bool Equals(PPChart c1, PPChart c2)
{
var rvalue = c1.UID == c2.UID;
if (c1.ChartType != c2.ChartType)
{
rvalue = false;
}
return rvalue;
}
}
«слайде в списке А, но не в B» и «слайд в списке А, но не в B», что это разница? –