DateTime比较没有时间部分?

Ali*_*hşi 5 .net c# linq datetime date-comparison

我写了这段代码.但我想忽略时间,我只想比较一天.

from s in sayac_okumalari
where s.okuma_tarihi == startDate && s.sayac_id == sayac_id
group s by new { date = new DateTime(((DateTime)s.okuma_tarihi).Year, ((DateTime)s.okuma_tarihi).Month, ((DateTime)s.okuma_tarihi).Day, ((DateTime)s.okuma_tarihi).Hour, 1, 1) } into g
select new
{
     okuma_tarihi = g.Key.date,
     T1 = g.Sum(x => x.toplam_kullanim_T1),
     T2 = g.Sum(x => x.toplam_kullanim_T2),
     T3 = g.Sum(x => x.toplam_kullanim_T3)
};
Run Code Online (Sandbox Code Playgroud)

例如:

25.02.1987 == 25.02.1987
Run Code Online (Sandbox Code Playgroud)

Ram*_*esh 17

s.okuma_tarihi.Value.Date == startDate.Date.这应该允许您仅比较Date组件.

更新从注释中的讨论看起来用户正在使用NullableType.因此更新了NullableType的解决方案.


L.B*_*L.B 7

使用DateDateTime的属性.对于前者,

var date= DateTime.Now.Date;
Run Code Online (Sandbox Code Playgroud)