Retrieve date only from date and time values โ€‹โ€‹using entity structure

I want to get the date only from the database. I use the code below ... but it gets the date and time.

using (FEntities context = new FEntities()) { DateTime date = DateTime.Now; if (context.tblvalue.Any(x => x.date == date)) { } } 
+5
source share
3 answers

You can compare only the indicated parts:

 context.tblvalue.Any(x => x.date.Year == data.Year && x.date.Month == data.Month && x.date.Day == data.Day); 

EDIT: You can also try the following:

 context.tblvalue.Any(x => EntityFunctions.TruncateTime(x.date) == data.Date); 

Another approach:

 context.tblvalue.Any(x => SqlFunctions.DatePart("year", x.date) == data.Year && SqlFunctions.DatePart("month", x.date) == data.Month && SqlFunctions.DatePart("day", x.date) == data.Day); 
+17
source

With entity infrastructure 6 and upgrade use

 context.tblvalue.Any(x => DbFunctions.TruncateTime(x.date) == data.Date); 
+2
source
 context.Database.SqlQuery<T>(sql, sql_parameters) 

accepts pure SQL in the first parameter. Use the MS SQL CONVERT server function (VARCHAR (6), date_value, 112) AS MonthYYYYmm to get the year with the month.

0
source

All Articles