2012-12-08 198 views
2

我需要同步在線數據庫和本地數據庫的約會。 這是我到目前爲止的代碼:比較列表中對象的屬性

 List<Appointment> onlineAppointments = new List<Appointment>(); 
     List<Appointment> localAppointments = new List<Appointment>(); 
     Appointment appointment01 = new Appointment(new DateTime(2012, 12, 24, 17, 30, 00), new DateTime(2012, 12, 24, 17, 45, 00), name, 123, "comment", 0, "test", 123, 1, DateTime.Now); 
     Appointment appointment02 = new Appointment(new DateTime(2012, 12, 24, 17, 30, 00), new DateTime(2012, 12, 24, 17, 45, 00), name, 123, "comment", 0, "test", 123, 1, DateTime.Now); 

     onlineAppointments.Add(appointment01); 
     localAppointments.Add(appointment02); 

因爲我只是想比較我創立的IEqualityComparer對象的某些屬性:

public class AppointmentEqualityComparer<T> : IEqualityComparer<T> where T : Appointment 
{ 
    #region IEqualityComparer<T> Members 

    public bool Equals(T x, T y) 
    { 
     return (x == null && y == null) || ((x != null && y != null) && 
     (x.getAppointmentStart() == y.getAppointmentStart() && 
     x.getAppointmentEnd() == y.getAppointmentEnd()) 
     ); 
    } 

    /// </exception> 
    public int GetHashCode(T obj) 
    { 
     if (obj == null) 
     { 
      throw new ArgumentNullException("obj"); 
     } 

     return obj.GetHashCode(); 
    } 

    #endregion 
} 

不幸的是,這並不工作:

var comparer = new AppointmentEqualityComparer<Appointment>();  
IEnumerable<Appointment> diffOnlineOffline = onlineAppointments.Except(localAppointments, comparer); 

含義diffOnlineOffline不是空的,但它應該是因爲這兩個列表包含相同的約會。

有什麼想法?

+0

我沒有看到在EqualityComparer任何問題。您是否已經完成了執行並檢查了兩個約會的價值? – rae1

+0

您的哈希碼實現應該使用與您的相等比較相同的屬性,即'AppointmentStart'和'AppointmentEnd'。 – Lee

+0

你是什麼意思@Lee?對不起,但我不太明白你的意思......你能給我一個具體的例子嗎? – libjup

回答

2

GetHashCode方法應該使用用於平等性。目前,你想要考慮的對象可能不會有相同的哈希碼。

您可以使用這樣的事情:

public int GetHashCode(T obj) 
{ 
    return 41 * (41 * (41 * (41 + obj.getAppointmentStart().GetHashCode())) 
    + obj.getAppointmentEnd().GetHashCode()); 
} 
1

我懷疑問題是與getAppointmentStartgetAppointmentEnd方法。下面是使用相同的相等比較你的代碼的簡潔版,但通過作爲預期性:

public class Appointment 
{ 
    private int Id { get; set; } 
    public DateTime Start { get; set; } 
    public DateTime End { get; set; } 

    public Appointment(int id, DateTime start, DateTime end) 
    { 
     Start = start; 
     End = end; 
     Id = id; 
    } 
} 


public class AppointmentEqualityComparer<T> : IEqualityComparer<T> where T : Appointment 
{ 
    #region IEqualityComparer<T> Members 

    public bool Equals(T x, T y) 
    { 
     return (x == null && y == null) 
       || ((x != null && y != null) && (x.Start == y.Start && x.End == y.End)); 
    } 

    public int GetHashCode(T obj) 
    { 
     if(obj == null) 
     { 
      throw new ArgumentNullException("obj"); 
     } 

     return obj.GetHashCode(); 
    } 

    #endregion 
} 

和實現:

var onlineAppointments = new List<Appointment>(); 
var localAppointments = new List<Appointment>(); 
var appointment01 = new Appointment(1, new DateTime(2012, 12, 24, 17, 30, 00), 
             new DateTime(2012, 12, 24, 17, 45, 00)); 
var appointment02 = new Appointment(2, new DateTime(2012, 12, 24, 17, 30, 00), 
             new DateTime(2012, 12, 24, 17, 45, 00)); 

onlineAppointments.Add(appointment01); 
localAppointments.Add(appointment02); 

var comparer = new AppointmentEqualityComparer<Appointment>(); 
var diffOnlineOffline = onlineAppointments.Except(localAppointments, comparer).ToList(); 

diffOnlineOffline只顯示了第一次約會。這導致getAppointmentStartgetAppointmentEnd方法正在返回除構造函數中使用的實際日期以外的值。

+0

感謝您的回答! 但它仍然無效:( foreach(預約diffOnlineOffline約會) { MessageBox。顯示(「列表中至少有1個約會」); } 仍然執行... – libjup

+0

找出它的工作原理。只需要實施@李的解決方案以及...但無論如何謝謝! – libjup

+0

根據2個樣本列表,您在'diffOnlineOffline'中預期會有多少個約會? –

0

你需要在你的約會類來實現的IEqualityComparer

+1

相同您可以將EqualityComparer傳遞給Except(這是OP中發生的事情),所以不需要在域類 –

+0

中實現IEqualityComaprer啊謝謝澄清! – Nikos