Please help. I am trying to figure out how to use DATE or DATETIME for comparison in a linq query.
Example: If I wanted all Employee names for those who started before today, I would do something like this in SQL:
SELECT EmployeeNameColumn
FROM EmployeeTable
WHERE StartDateColumn.Date <= GETDATE() //Today
But what about linq?
DateTime startDT = //Today
var EmployeeName =
from e in db.employee
where e.StartDateColumn <= startDT
The above WHERE doesn't work:
Exception Details: System.NotSupportedException: The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Use the class DbFunctions
for trimming the time portion.
using System.Data.Entity;
var bla = (from log in context.Contacts
where DbFunctions.TruncateTime(log.ModifiedDate)
== DbFunctions.TruncateTime(today.Date)
select log).FirstOrDefault();