Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq where clause compare only date value without time value

var _My_ResetSet_Array = _DB     .tbl_MyTable     .Where(x => x.Active == true         && x.DateTimeValueColumn <= DateTime.Now)     .Select(x => x); 

Upper query is working correct.
But I want to check only date value only.
But upper query check date + time value.

In traditional mssql, I could write query like below.

SELECT * FROM dbo.tbl_MyTable WHERE  CAST(CONVERT(CHAR(10), DateTimeValueColumn, 102) AS DATE) <=              CAST(CONVERT(CHAR(10),GETDATE(),102) AS DATE) AND Active = 1 

So could anyone give me suggestion how could I check only date value in Linq.

like image 459
Frank Myat Thu Avatar asked Nov 22 '12 10:11

Frank Myat Thu


People also ask

How to compare two Dates in Linq query without Time?

StartDate >= DateTime. Now). OrderBy(d => d. StartDate);

How do I compare two dates in LINQ query with time?

Meeting dates are stored in this table using the following format: May 2nd 2011 is (for example) formatted as 5/2/2011 . My requirement is to get the meetings between two dates (say, 4/25/2011 and 5/2/2011) and to write a query comparing a date with these two dates. Does it compare like 4/25/2011 < 4/26/2011?


2 Answers

There is also EntityFunctions.TruncateTime or DbFunctions.TruncateTime in EF 6.0

like image 69
Johann Blais Avatar answered Sep 23 '22 00:09

Johann Blais


Simple workaround to this problem to compare date part only

var _My_ResetSet_Array = _DB                     .tbl_MyTable                     .Where(x => x.Active == true &&                                 x.DateTimeValueColumn.Year == DateTime.Now.Year                             && x.DateTimeValueColumn.Month == DateTime.Now.Month                             && x.DateTimeValueColumn.Day == DateTime.Now.Day); 

Because 'Date' datatype is not supported by linq to entity , where as Year, Month and Day are 'int' datatypes and are supported.

like image 31
Pranay Rana Avatar answered Sep 21 '22 00:09

Pranay Rana