How to compare DateTime without time via LINQ?

前端 未结 9 623
夕颜
夕颜 2020-12-08 18:03

I have

var q = db.Games.Where(t => t.StartDate >= DateTime.Now).OrderBy(d => d.StartDate);

But it compares including time part of

9条回答
  •  轮回少年
    2020-12-08 18:57

    I found this question while I was stuck with the same query. I finally found it without using DbFunctions. Try this:

    var q = db.Games.Where(t => t.StartDate.Day == DateTime.Now.Day && t.StartDate.Month == DateTime.Now.Month && t.StartDate.Year == DateTime.Now.Year ).OrderBy(d => d.StartDate);

    This way by bifurcating the date parts we effectively compare only the dates, thus leaving out the time.

    Hope that helps. Pardon me for the formatting in the answer, this is my first answer.

提交回复
热议问题