5

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

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

例如:

25.02.1987 == 25.02.1987
4

5 回答 5

17

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

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

于 2012-07-24T06:51:42.823 回答
8

使用DateDateTime 的属性。例如,

var date= DateTime.Now.Date;
于 2012-07-24T06:52:20.873 回答
2

因为您可以转换s.okuma_tarihi为 DateTime,所以我认为您可以这样做:

var sonuc = from s in sayac_okumalari
        where (DateTime)s.okuma_tarihi.Date == startDate.Date && 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)
        };

希望能帮助到你。

于 2012-07-24T07:11:43.703 回答
0

尝试在 DateTime 对象上使用 Date 属性将是一个很好的简单解决方案。

string AdmissionDate = "10/15/2017" // mm/dd/yyyy
string DepartureDate = "10/14/2017" // mm/dd/yyyy

if (Convert.ToDateTime(AdmissionDate).Date > Convert.ToDateTime(DepartureDate).Date)
{
    // Validation: Admission Date can not be greater than Departure Date... 
}
于 2017-09-03T20:10:09.723 回答
-1

试试这个...

        try
        {
         DateTime Date1= DateTime.ParseExact(txtBox1.Text,"dd/MM/yyyy",null);
         DateTime Date2 = DateTime.ParseExact(txtBox2.Text, "dd/MM/yyyy", null);

          if (Date1 > Date2) 
          {
           //........
          }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
于 2017-07-11T03:01:04.707 回答