Use
CreateTime(hour, minute, second)
Question
I have datetime variable officeStartTime
which hold datetime value as
officeStartTime = {9/24/2013 10:00:00 AM}
and I have module CHECKINOUT
which contain userid
, checktime
and other properties.
Now what I want is to get list of CHECKINOUT
entries whose time part of checktime is greater than time part of officeStartTime
.
I try:
var checklist= con.CHECKINOUTs.Where(x => x.CHECKTIME.TimeOfDay > officeStartTime.TimeOfDay);
checklist
contains the following error is shown:
{"The specified type member 'TimeOfDay' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."}
.
It says TimeOfDay
is not supported in LINQ to Entities, if so how can I check for time part of datetime in LINQ. Is there any other way to do this? Please suggest me what to do.
Thank you.
La solution
Autres conseils
Use EntityFunctions.CreateTime method:
var checklist= from c in con.CHECKINOUTs
let time = EntityFunctions.CreateTime(c.CHECKTIME.Hour,
c.CHECKTIME.Minute,
c.CHECKTIME.Second)
where time > officeStartTime.TimeOfDay
select c;
Fluent syntax:
con.CHECKINOUTs.Where(c => EntityFunctions.CreateTime(c.CHECKTIME.Hour, c.CHECKTIME.Minute, c.CHECKTIME.Second) > officeStartTime.TimeOfDay)
Just as a heads up, if you are using Entity Framework Version 6.0.0.0, EntityFunctions
is OUTDATED.
The same functionality can be found under DbFunctions
.
So for your answer, using EF6, it would look like this:
con.CHECKINOUTs.Where(c => DbFunctions.CreateTime(c.CHECKTIME.Hour, c.CHECKTIME.Minute, c.CHECKTIME.Second) > officeStartTime.TimeOfDay)
Hope this helps!