我必須比較兩個包含另一個項目列表(稱爲圖表)的類型幻燈片列表。 我必須找到幻燈片清單,其中的差異可能是之間的所有差異:比較兩個列表,並獲取所有區別?
在列表A中的幻燈片,而不是B中
在列表A中的幻燈片,而不是B中
幻燈片在這兩個列表中,但它們的圖表不同
我嘗試使用except,但它返回具有不同圖表的列表爲「相同」。 示例 列表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;
}
}
「一表A幻燈片,但不是在B」和 「中列出的滑動,但不是在B」是什麼是不同的? –