Get time only for datetime

I have a datetime variable officeStartTime in which the datetime value is specified as

officeStartTime = {9/24/2013 10:00:00 AM}

and I have a CHECKINOUT module that contains userid , checktime and other properties.

Now I want to get a list of CHECKINOUT records whose part of the scan time is longer than the temporary part of officeStartTime .

I'm trying to:

var checklist= con.CHECKINOUTs.Where(x => x.CHECKTIME.TimeOfDay > officeStartTime.TimeOfDay);

checklist contains the following error:

{"The specified type member 'TimeOfDay' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."} .

He says that TimeOfDay not supported in LINQ for Entities, if so, how can I check the time portion of the datetime in LINQ. Is there any other way to do this? Please suggest me what to do.

Thanks.

+7
linq entity-framework
source share
3 answers

Use

 CreateTime(hour, minute, second) 

from Calendar functions of date and time

+11
source share

Use the 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; 

Free syntax:

 con.CHECKINOUTs.Where(c => EntityFunctions.CreateTime(c.CHECKTIME.Hour, c.CHECKTIME.Minute, c.CHECKTIME.Second) > officeStartTime.TimeOfDay) 
+9
source share

As with Entity Framework version 6.0.0.0, EntityFunctions is OUTDATED .

The same functionality can be found in the DbFunctions section.

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!

+4
source share

All Articles