嗯,我正面临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 );
} …Run Code Online (Sandbox Code Playgroud)