【问题标题】:DateTime comparison without time part?没有时间部分的日期时间比较?
【发布时间】:2012-07-22 10:58:53
【问题描述】:

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

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

【问题讨论】:

标签: c# .net linq datetime date-comparison


【解决方案1】:

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

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

【讨论】:

  • 's.okuma_tarihi' 不包含“日期”的定义
  • 从您的代码 sn-p 看起来,您需要将 s.okuma_tarihi 类型转换为 DateTime
  • s.okuma_tarihi.Value.Date == startDate.Date 正在工作。如果编辑您的解决方案,我会标记它。谢谢。
  • ((DateTime)s.okuma_tarihi)。日期仅使用调用,分配。
  • 已更新。但是,看起来您使用的是 Nullable 类型。在执行操作之前,您可能需要确保 Nullable 类型具有值。
【解决方案2】:

使用日期时间的Date 属性。例如,

var date= DateTime.Now.Date;

【讨论】:

    【解决方案3】:

    因为您可以将 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)
            };
    

    希望对你有帮助。

    【讨论】:

      【解决方案4】:

      尝试在 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... 
      }
      

      【讨论】:

        【解决方案5】:

        试试这个...

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-07-02
          • 2017-10-12
          • 2021-02-09
          • 2010-09-11
          • 2011-08-29
          • 1970-01-01
          • 1970-01-01
          • 2011-12-02
          相关资源
          最近更新 更多