如何使用linq查询字段类型是时间并向其添加20分钟 [英] How to use linq to query field type is time and add 20 mins to it

查看:148
本文介绍了如何使用linq查询字段类型是时间并向其添加20分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张桌子,其中一张是用于外地的预订表.并将用户飞行时间插入此表的预订中,

另一个表称为公交时刻表,一旦客户将航班时间插入到预订表中,那么我需要写一个查询来查找该客户是否有合适的时间,如果有的话就获得价值清单.

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.

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.



问题是如何在linq中格式化时间跨度,以及如何向我的时间跨度添加30分钟



Question is how to format timespan in linq and how to add 30 mins to my timespan

推荐答案

请参考DateTime.ToString方法(字符串) [
refer DateTime.AddMinutes Method[^] and DateTime.ToString Method (String)[^] those methods will help you to do the task.
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,

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") };



请参考
TimeSpan.Add方法 [ ^ ]和自定义TimeSpan格式字符串 [



refer TimeSpan.Add Method[^] and Custom TimeSpan Format Strings[^]


它解决了,我把它放在这里,希望能对其他人有所帮助:
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();


这篇关于如何使用linq查询字段类型是时间并向其添加20分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆