Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get time only of datetime

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.

like image 201
link2jagann Avatar asked Sep 30 '13 11:09

link2jagann


2 Answers

Use

CreateTime(hour, minute, second)

from Date and Time Canonical Functions

like image 138
MikroDel Avatar answered Sep 20 '22 03:09

MikroDel


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)
like image 37
Sergey Berezovskiy Avatar answered Sep 23 '22 03:09

Sergey Berezovskiy