Click here to Skip to main content
15,847,903 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 2 tables, one is the booking table for use to field. and will insert the user flight time to this table booking,

another table is named bus timetable, once the client insert the flight time to the booking table, then I need to write a query to find if there are time suit for this client, if has then get the list of value.

C#
airportTimetable = from r in db.ShuttleTimeTables 
                    where r.TimeTypeId == 1 && r.Time >= flightTime + (30min)
                    select new SelectListItem 
                  { 
                      Value = r.Time.ToString(), 
                      Text = r.Time.ToString() };

// and the time need format,I don't know how to format it.
// output look is like this 08:00:00.0000000 at the moment,
// it is ugly.


Question is how to format timespan in linq and how to add 30 mins to my timespan
Posted
Updated 29-Oct-14 17:54pm
v2

refer DateTime.AddMinutes Method[^] and DateTime.ToString Method (String)[^] those methods will help you to do the task.
C#
DateTime time = flightTime.AddMinute(30);

airportTimetable = from r in db.ShuttleTimeTables 
                    where r.TimeTypeId == 1 && r.Time >= time
                    select new SelectListItem 
                  { 
                      Value = r.Time.ToString(), 
                      Text = r.Time.ToString("HH:mm:ss") };


if you have timespan then,

C#
TimeSpan time = flightTime.Add(TimeSpan.FromMinutes(30));

airportTimetable = from r in db.ShuttleTimeTables 
                    where r.TimeTypeId == 1 && r.Time >= time
                    select new SelectListItem 
                  { 
                      Value = r.Time.ToString(), 
                      Text = r.Time.ToString(@"hh\:mm\:ss") };


refer TimeSpan.Add Method[^] and Custom TimeSpan Format Strings[^]
 
Share this answer
 
v6
Comments
SandyZhang2014 29-Oct-14 23:01pm    
How to use it?? do you have an example?
SandyZhang2014 29-Oct-14 23:03pm    
The field just have time, don't have date, I don't know how to use it.
SandyZhang2014 29-Oct-14 23:06pm    
DateTime time = flightTime.AddMinute(30);this doen't work,
the flightTime return timespan
SandyZhang2014 29-Oct-14 23:24pm    
The query is work, but the format don't work
DamithSL 29-Oct-14 23:27pm    
what is the format you want to show? check the documentation and change the format as you wish.
It solved, I put it here, hope that will help someone else:
<pre lang="c#">
TimeSpan time = flightTime.Add(TimeSpan.FromMinutes(30));
 
airportTimetable = (from r in db.ShuttleTimeTables
                    where r.TimeTypeId == 3 && r.Time >= time2
                    select r.Time)
                    .ToList()
                    .Select(x => new SelectListItem() 
                     {
                        Text = x.Value.ToString(@"hh\:mm"),
                        Value = x.Value.ToString(@"hh\:mm")
                      })
                   .ToList();
 
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