Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I want to filter out the time based uplon query
I am retreving From Time and ToTime from the database
it is coming like this
VB
FromTime         ToTime
2:30 PM          4:30 PM
11:30 AM        11:30 AM
5:30 PM          7:30 PM
4:30 PM           1:30 AM
1:30 AM           2:30 AM
6:30 AM          12:30 PM
10:30 PM         8:30 AM

I have written linq query which query's the datatable
and filters out Time Between 12:00 Am and 09:00 AM
C#
var time = from i in dt.AsEnumerable()
                          where DateTime.Parse(i.Field<string>("FromTime"), CultureInfo.InvariantCulture) >= DateTime.Parse("12:00 AM", CultureInfo.InvariantCulture)
                              && DateTime.Parse(i.Field<string>("ToTime"), CultureInfo.InvariantCulture) <= DateTime.Parse("09:00 AM", CultureInfo.InvariantCulture)
                          select i;


But the filtering is not done properly can any one let me know where i am going wrong
Posted
Updated 30-Sep-13 3:26am
v2
Comments
joshrduncan2012 30-Sep-13 9:23am    
"But the filtering is not done properly"
Can you elaborate on this? What output are you getting as compared to what you expect to get?
rocky_em 30-Sep-13 9:47am    
wheni execute the query i amgetting out put like this
From Time Totime
4:30 PM 1:30 AM
1:30 AM 2:30 AM
10:30 PM 8:30 AM

what i am expecting is
FromTime ToTime
1:30 AM 2:30 AM
because i want record between 12:00 AM and 09:00 AM
joshrduncan2012 30-Sep-13 9:52am    
You should probably change && DateTime.Parse(i.Field<string>("ToTime") to filter on "FromTime". That's probably why you are getting incorrect information.
rocky_em 30-Sep-13 9:54am    
I am Sorry i did not understood what you meant to say
joshrduncan2012 30-Sep-13 10:07am    
My suggestion would be to do the following:

Change
&& DateTime.Parse(i.Field<string>("ToTime")
to read
&& DateTime.Parse(i.Field<string>("FromTime")

1 solution

it looks like your query is working correctly based on how it is written
you said:
FromTime >= 12AM
and ToTime <= 9AM

The result set you are getting has FromTime >= 12AM (4:30PM, 1:30AM and 10:30PM)
and ToTime <= 9AM (1:30 AM, 2:30AM, 8:30AM)

if you want BETWEEN try
(FromTime >= 12AM && FromTime <= 9AM)
AND (ToTime >= 12AM && ToTime <= 9AM)
 
Share this answer
 

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