基本上,用户将在特定日期预留时间。例如2016年3月21日上午10:00到2016年3月21日下午1点。如果要进行另一次预约,系统不应接受2016年3月21日上午10点到下午1点之间的时间,因为它已经保留给另一个用户。请帮助我茫然,有点新的编程。我坚持这个查询
public Boolean CheckExistingTime(string reason, string coursegrade, string section, DateTime start, DateTime end)
{
try
{
using (IHSEntities model = new IHSEntities())
{
var list = from sched in model.Schedules
where sched.Reason == reason && sched.CourseGrade == coursegrade && sched.Section == section
select new ScheduleList
{
scheduleid = sched.ScheduleID,
};
if (list.ToList().Count > 0)
{
return true;
}
else
{
return false;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
这是计划表的设计
I am using Entity Framework, so here is the complete Schedule Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data;
namespace Business
{
public class Schedule
{
public class ScheduleList
{
public int scheduleid { get; set; }
public string coursegrade { get; set; }
public string section { get; set; }
public string reason { get; set; }
public string date { get; set; }
public string level { get; set; }
public string adviser { get; set; }
public string starttime { get; set; }
public string endtime { get; set; }
public string inputtedby { get; set; }
}
public List<ScheduleList> GetAllSchedule(DateTime date)
{
try
{
using (IHSEntities model = new IHSEntities())
{
var list = from schedule in model.Schedules
where schedule.Date >= date
select new ScheduleList
{
scheduleid = schedule.ScheduleID,
coursegrade = schedule.CourseGrade,
section = schedule.Section,
reason = schedule.Reason,
level = schedule.Levels,
date = schedule.Date.ToString(),
inputtedby = schedule.InputedBy,
};
return list.ToList();
}
}
catch (Exception ex)
{
throw ex;
}
}
public List<ScheduleList> GetAllToday(DateTime date)
{
try
{
using (IHSEntities model = new IHSEntities())
{
var list = from schedule in model.Schedules
where schedule.Date == date orderby schedule.starttime
select new ScheduleList
{
scheduleid = schedule.ScheduleID,
coursegrade = schedule.CourseGrade,
section = schedule.Section,
reason = schedule.Reason,
level = schedule.Levels,
starttime = schedule.starttime.ToString(),
endtime = schedule.endtime.ToString(),
date = schedule.Date.ToString(),
inputtedby = schedule.InputedBy,
};
return list.ToList();
}
}
catch (Exception ex)
{
throw ex;
}
}
public Boolean CheckExistingTime(string reason, string coursegrade, string section, DateTime start, DateTime end)
{
try
{
using (IHSEntities model = new IHSEntities())
{
var list = from sched in model.Schedules
where sched.Reason == reason && sched.CourseGrade == coursegrade && sched.Section == section && sched.starttime <= start && sched.endtime >= end
select new ScheduleList
{
scheduleid = sched.ScheduleID,
};
if (list.ToList().Count > 0)
{
return true;
}
else
{
return false;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
public ScheduleList GetSchedule(int id)
{
try
{
using (IHSEntities model = new IHSEntities())
{
var list = (from schedule in model.Schedules
where schedule.ScheduleID == id
select new ScheduleList
{
coursegrade = schedule.CourseGrade,
section = schedule.Section,
reason = schedule.Reason,
date = schedule.Date.ToString(),
level = schedule.Levels,
adviser = schedule.Senttoadviser,
inputtedby = schedule.InputedBy,
}).First();
return list;
}
}
catch (Exception ex)
{
throw ex;
}
}
public Boolean Delete(int id)
{
try
{
using (IHSEntities model = new IHSEntities())
{
foreach (Data.Schedule sched in model.Schedules.Where(x => x.ScheduleID == id))
model.Schedules.Remove(sched);
model.SaveChanges();
return true;
}
}
catch (Exception ex)
{
throw ex;
}
}
public Boolean Update(int id, DateTime newdate)
{
try
{
using (IHSEntities model = new IHSEntities())
{
var list = (from sched in model.Schedules
where sched.ScheduleID == id
select sched).First();
list.Date = newdate;
model.SaveChanges();
}
return true;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
答案 0 :(得分:3)
这应该可以解决问题:
using (var model = new IHSEntities())
{
bool result =
model.Schedules.Where(s => s.Section == section && s.CourseGrade == coursegrade
&& s.Reason == reason)
.Any(s => (s.starttime >= start && s.starttime <= end) ||
(s.endtime <= end && s.endtime >= start));
return result;
}
如果有任何记录,其中起始时间位于start&amp;停止或,其中结束时间位于start&amp;停止这将返回true。
编辑:这只有在您没有保存您的结束时间和开始时间且日期部分被截断的情况下才有效,否则您需要对日期进行额外检查。 (但请不要删除它们的日期部分,那么甚至不需要你的日期字段)
答案 1 :(得分:1)
你快到了。唯一的错误是这种情况
sched.starttime <= start && sched.endtime >= end
您需要检测两个范围(包括起始和独占结束)是否相交(重叠)。正确的条件是
sched.starttime < end && sched.endtime > start
此外,尽管并不重要,但在此特定函数中,您不需要整个重叠项列表,只需检查是否存在至少一个,在LINQ中由Any
方法表示。< / p>
所以你可以使用这样的东西:
var query = from sched in model.Schedules
where sched.Reason == reason &&
sched.CourseGrade == coursegrade &&
sched.Section == section &&
sched.starttime < end && sched.endtime > start
select sched;
return query.Any();
答案 2 :(得分:0)
您应该在.Net框架中使用DateTime.Compare方法。这将比较DateTime的两个实例并返回一个整数,该整数指示第一个实例是否早于,等于或晚于第二个实例。所以它基本上与时间部分进行比较也是你需要的部分。
https://msdn.microsoft.com/en-us/library/system.datetime.compare(v=vs.110).aspx
如果您添加日程表模型,我可以为您编写查询。