Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi dude,

I have LINQ query for compare 2 times,

Inputs: Opening time  : 09:00 and Closing time  : 21:00



Table fields are,

  Id        Openingtime         Closingtime
  1         08:45                 20:00
  2         09:00                 19:30
  3         10:00                 21:00
  4         9:45                  20:30
  5         9:15                  22:00


My Query Is,

in between time how i get,

results:
3         10:00                 21:00
5         9:15                  22:00


reply me.
Posted
Updated 24-Apr-12 0:45am
v2
Comments
SASS_Shooter 23-Apr-12 12:50pm    
You show everything but your actual linq statement!!! How can we answer your question without
a) knowing your query you tried
b) what you are getting instead of the desired results.

Know also that if this is a school question (which this smacks of) we will not do your school work for you.

The following code can be used to filter the data as required
C#
void Main()
{
    DataTable timeData = new DataTable();
    timeData.Columns.Add("Id",typeof(int),null);
    timeData.Columns.Add("OpeningTime",typeof(TimeSpan),null);
    timeData.Columns.Add("ClosingTime",typeof(TimeSpan),null);
    
    timeData.Rows.Add(1,new TimeSpan(8,45,0),new TimeSpan(20,0,0));
    timeData.Rows.Add(2,new TimeSpan(9,0,0),new TimeSpan(19,30,0));
    timeData.Rows.Add(3,new TimeSpan(10,0,0),new TimeSpan(21,0,0));
    timeData.Rows.Add(4,new TimeSpan(9,45,0),new TimeSpan(20,30,0));
    timeData.Rows.Add(5,new TimeSpan(9,15,0),new TimeSpan(22,0,0));
    
    TimeSpan openingTime = new TimeSpan(9,0,0);
    TimeSpan closingTime = new TimeSpan(21,0,0);
    
    var enclosedTime = timeData.AsEnumerable().Where (
    	d => d.Field<timespan>("OpeningTime").CompareTo(openingTime) >=0 
        	&& d.Field<timespan>("ClosingTime").CompareTo(closingTime) >=0)
    	.Select (d => d).CopyToDataTable();
}
 
Share this answer
 
v3
Comments
Abhinav S 23-Apr-12 23:18pm    
5!
VJ Reddy 23-Apr-12 23:23pm    
Thank you, Abhinav.
El_Codero 24-Apr-12 18:43pm    
Nice. My 5!
VJ Reddy 24-Apr-12 18:49pm    
Thank you, Björn.
I am replying you. Since that is the closest thing to a question that you asked.
 
Share this answer
 
This is SQL QUERY:

SQL
select Count(*) from restauranttimings where (Convert(varchar(8),openingtime,108)<CONVERT(VARCHAR(8),GETDATE(),108)) and
         (Convert(varchar(8),closingtime,108)>CONVERT(VARCHAR(8),GETDATE(),108)) and holidayreason!='NULL' and days='tuesday'
 
Share this answer
 
THIS is LinQ Query:


C#
var Query = (from rt in db.RestaurantTimings
                         where rt.Days == System.DateTime.Now.DayOfWeek.ToString()
                         && Convert.ToDateTime(rt.OpeningTime).TimeOfDay.Ticks < Convert.ToDateTime(HoursMinutes).TimeOfDay.Ticks
                         && Convert.ToDateTime(rt.ClosingTime).TimeOfDay.Ticks > Convert.ToDateTime(HoursMinutes).TimeOfDay.Ticks
                         && (rt.HolidayReason != null || rt.HolidayReason != "")
                         select (rt.RestaurantIndex_Id)).Count();
            return Query;
 
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