DateTime comparison without time part?

18,851

Solution 1

use s.okuma_tarihi.Value.Date == startDate.Date. This should allow you to compare only the Date component.

Update From the discussion in comments looks like the user is using NullableType. Hence updated the solution for NullableType.

Solution 2

Use Date property of DateTime. For ex,

var date= DateTime.Now.Date;

Solution 3

Because you could convert s.okuma_tarihi to DateTime, I think you could do:

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

Hope it helps.

Share:
18,851
AliRıza Adıyahşi
Author by

AliRıza Adıyahşi

https://github.com/alirizaadiyahsi Contact : [email protected]

Updated on June 21, 2022

Comments

  • AliRıza Adıyahşi
    AliRıza Adıyahşi almost 2 years

    I wrote this code. But I want to ignore time, I want to compare only day.

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

    for example:

    25.02.1987 == 25.02.1987
    
  • AliRıza Adıyahşi
    AliRıza Adıyahşi almost 12 years
    's.okuma_tarihi' does not contain a definition for 'Date'
  • Ramesh
    Ramesh almost 12 years
    Looks like from your code snippet, you need to typecast s.okuma_tarihi to DateTime
  • AliRıza Adıyahşi
    AliRıza Adıyahşi almost 12 years
    s.okuma_tarihi.Value.Date == startDate.Date is working.If edit your solution I will mark it. Thanks.
  • AliRıza Adıyahşi
    AliRıza Adıyahşi almost 12 years
    ((DateTime)s.okuma_tarihi).Date use only calling, assigment.
  • Ramesh
    Ramesh almost 12 years
    Updated. But, looks like you are using Nullable type. You may need to ensure that Nullable type has value before performing action.
  • AliRıza Adıyahşi
    AliRıza Adıyahşi almost 12 years
    This is usefull knowledge for me. Thanks a lot.