Click here to Skip to main content
15,077,931 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have DateTime column in my database I use following query to get the data of specific Date,

List<Products> AllProductsByGivenDate(DateTime forDate)
       var query = ctx.Products.Where(p=>p.DateTime.Value.Year==forDate.Year && p.DateTime.Value.Month==forDate.Month && p.DateTime.Value.Day==forDate.Day).ToList();
       return query;

Now I further wants to retrieve the data for specific Time Interval like,

12:00 AM to 08:30 AM

8:30 AM to 11:45 AM

I mean I have the data of specific date now I need to select all records which exists from 12:00 AM to 08:30 AM

I use p.DateTime.Value.Hour>='08' && p.DateTime.Value.Hour<='12' && p.DateTime.Value.Minute>='00' && p.DateTime.Value.Minute<='59'

for a shift like 08:00 to 12:00 it works Perfect but for 8:30 it also shows the records for 8:31 to 8:59 but i need only records from 8:30 to 12:00.
Updated 27-Aug-14 9:17am
Gihan Liyanage 27-Aug-14 8:17am
Do you want dividing them to groups ?

First, you should have a correct understanding of "Hour>=08 && Hour<=12 && Minute>=00 && Minute<=59" means the period between 08:00 and 12:59.

But,"Hour>=08 && Hour<=12 && Minute>=30 && Minute<=59" means 08:30~8:59,09:30~9:59,...,12:30~12:59.

Get this? Hour and minute can be not divided!

If you want to make it between 8:30 and 12:00, you have to treat your p.DateTime as a "DateTime" instead of a "String" or "Int".

Now, the rest is your work.
shaprpuff 28-Aug-14 7:56am

This code will show all records from 7:30 to 16:00

TimeSpan startTime= new TimeSpan(7, 30, 0);
TimeSpan endTime= new TimeSpan(16, 00, 0);

List<Products> shift= ctx.Products.Where(p => p.DateTime.Value.TimeOfDay >= startTime && p.DateTime.Value.TimeOfDay <= endTime).ToList();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900