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 不为空,但应该是因为两个列表都包含相同的约会。

任何想法?

4

3 回答 3

2

您的GetHashCode方法应该使用用于相等的属性。目前,您想要认为相等的对象可能不会具有相同的哈希码。

你可以使用这样的东西:

public int GetHashCode(T obj)
{
    return 41 * (41 * (41 * (41 + obj.getAppointmentStart().GetHashCode())) 
    + obj.getAppointmentEnd().GetHashCode());
}
于 2012-12-08T17:57:05.090 回答
1

我怀疑问题出在getAppointmentStartandgetAppointmentEnd方法上。这是您的代码的简洁版本,具有相同的相等比较器,但使用按预期工作的属性:

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();

wherediffOnlineOffline只显示第一次约会。这导致得出的结论是, getAppointmentStartandgetAppointmentEnd方法返回的值不是构造函数中使用的实际日期。

于 2012-12-08T17:52:30.410 回答
0

您需要在 Appointment 类中实现 IEqualityComparer

于 2012-12-08T17:53:11.080 回答