我有以下類:兩個班在LINQ比較 - 讓「錯配」
public class DocumentCompare
{
public string Customer;
public string Filename;
public string Reference;
public DateTime? Date;
public override bool Equals(object obj)
{
if (obj == null)
return false;
DocumentCompare doc = obj as DocumentCompare;
if ((Object)doc == null)
return false;
return (doc.Customer == Customer) && (doc.Date == Date) && (doc.Filename == Filename) && (doc.Reference == Reference);
}
public bool Equals(DocumentCompare doc)
{
if ((object)doc == null)
return false;
return (doc.Customer == Customer) && (doc.Date == Date) && (doc.Filename == Filename) && (doc.Reference == Reference);
}
public override int GetHashCode()
{
return string.Format("{0}_{1}_{2}_{3}",Customer,Filename,Reference,(Date == null ? "" : Date.Value.ToString())).GetHashCode();
}
}
我會抓取這個類的2只列出了 - 我想要做的就是兩者進行比較,並獲得那些這兩者都不存在。所以如果一個項目存在於x列表中但不存在於y中,我想對這個列表中的項目執行一個動作。如果一個項目存在於y列表中但不存在於x中,我想要執行不同的操作。
我該怎麼做?我猜是使用LINQ!
編輯:性能不是大問題 - 這將只運行一次
+1非常同意拋開3:通常最好保留默認的引用等式,或者引入代理鍵,並且如果您絕對必須進行比較 – MattDavey 2012-02-06 09:44:41
謝謝 - 我知道這不是最好的解決方案,但這隻會運行一次 – Chris 2012-02-07 08:29:47