IEquatable (C#) の問題に直面しています。次のコードでわかるように、IEquatable を実装したクラスを取得しましたが、「Equals」メソッドに到達していません。私の目的は次のとおりです。データベースに日時列があり、「時間」の部分を考慮せずに日付のみを区別したいと考えています。
例: 12-01-2014 23:14 は 12-01-2014 18:00 と同じです。
namespace MyNamespace
{
public class MyRepository
{
public void MyMethod(int id)
{
var x = (from t in context.MyTable
where t.id == id
select new MyClassDatetime()
{
Dates = v.Date
}).Distinct().ToList();
}
}
public class MyClassDatetime : IEquatable<MyClassDatetime>
{
public DateTime? Dates { get; set; }
public bool Equals(MyClassDatetime other)
{
if (other == null) return false;
return (this.Dates.HasValue ? this.Dates.Value.ToShortDateString().Equals(other.Dates.Value.ToShortDateString()) : false);
}
public override bool Equals(object other)
{
return this.Equals(other as MyClassDatetime );
}
public override int GetHashCode()
{
int hashDate = Dates.GetHashCode();
return hashDate;
}
}
}
適切に機能させる方法や、必要なことを行うための他のオプションを知っていますか?? ありがとうございました!!