C# 使用日期值计算元素
我有一个DB表,叫做DBAppoints。该数据库由两个字段组成:开始日期和结束日期。我想做的是计算在某个时间间隔内我有多少约会。例如,我的表中有两个约会:C# 使用日期值计算元素,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我有一个DB表,叫做DBAppoints。该数据库由两个字段组成:开始日期和结束日期。我想做的是计算在某个时间间隔内我有多少约会。例如,我的表中有两个约会: App. 1 : 8h30 -> 10h30 App. 2 : 9h -> 10h 更明确地说,我真正做的很简单:我想根据开始日期和持续时间插入一个新约会。要执行此操作,我必须检查是否可以从某个日期和时间添加约会。再举一个例子: 我想增加一个约会,其持续时间等于2小时。根据我的记录,我会一步一步地用半个小时来决定我能不能做到
App. 1 : 8h30 -> 10h30
App. 2 : 9h -> 10h
更明确地说,我真正做的很简单:我想根据开始日期和持续时间插入一个新约会。要执行此操作,我必须检查是否可以从某个日期和时间添加约会。再举一个例子:
我想增加一个约会,其持续时间等于2小时。根据我的记录,我会一步一步地用半个小时来决定我能不能做到
DBAppointment[] appointmentsOfCurrentDay = (from a in context.DBAppointments where a.StartDate.Value.Date == day.Date select a).ToArray();
foreach (DBAppointment dbapp in appointments)
{
DateTime end = dbapp.PatchExpiration.Value;
for (DateTime startTime = dbapp.StartDate.Value; startTime <= end; startTime = startTime.AddHours(0.5))
{
**int countAppointment = appointmentsOfCurrentDay.Count(a => a.StartDate <= startTime && startTime.AddHours(duration) <= a.EndDate);**
if (countAppointment < maxBeds && (Math.Ceiling(Decimal.ToDouble(dbapp.PatchQuantity.Value)) - Decimal.ToDouble(dbapp.PatchQuantity.Value) >= patchQuantity))
{
IntervalViewModel ivm = new IntervalViewModel();
ivm.StartDate = startTime;
ivm.EndDate = startTime.AddHours(duration);
listValidIntervals.Add(ivm);
}
}
dbappoint[]appointsofCurrentDay=(从context.dbappoints中的a.StartDate.Value.Date==day.Date选择a.ToArray();
foreach(任命中的dbapp)
{
DateTime end=dbapp.PatchExpiration.Value;
对于(DateTime startTime=dbapp.StartDate.Value;startTime a.StartDate我更改了代码,可能是这样的:
int countAppointment = appointmentsOfCurrentDay.Count(a => (startTime >= a.StartDate && startTime < a.EndDate || startTime.AddHours(duration) > a.StartDate && startTime.AddHours(duration) <= a.EndDate) ||
(a.StartDate >= startTime && a.StartDate < startTime.AddHours(duration) || a.EndDate > startTime && a.EndDate <= startTime.AddHours(duration)));
int countAppointment=appointmentsOfCurrentDay.Count(a=>(startTime>=a.StartDate&&startTimea.StartDate&&startTime.AddHours(duration)=startTime&&a.StartDatestartTime&&a.EndDate只看:
int countAppointment=appointsofCurrentDay.Count(a=>a.StartDate我写了这个小例子来解释我的评论,希望它能帮助解决您的问题。很抱歉懒得重新编写代码。如果您需要进一步的帮助,请告诉我
private List<DateTime> ValidEndTime = new List<DateTime>(); //Contains the list of validEndDate
private void button1_Click(object sender, EventArgs e)
{
List<DateTime> currentAppointment = new List<DateTime>();
currentAppointment.Add(Convert.ToDateTime("22 May 2014 14:22"));
currentAppointment.Add(Convert.ToDateTime("22 May 2014 14:30"));
foreach (DateTime endDate in currentAppointment)
{
if (IsAppointmentTimeValid(endDate) == true)
{
ValidEndTime.Add(endDate);
}
}
}
private bool IsAppointmentTimeValid(DateTime newAppointmentEndTime)
{
//compare max(maxendDateTime) with the newAppointmentDateTime
return ValidEndTime.Max(t => t.Date) < newAppointmentEndTime? true : false;
}
private List ValidEndTime=new List();//包含validEndDate的列表
私有无效按钮1\u单击(对象发送者,事件参数e)
{
List currentAppointment=新列表();
currentAppointment.Add(转换为ToDateTime(“2014年5月22日14:22”);
currentAppointment.Add(转换为ToDateTime(“2014年5月22日14:30”);
foreach(当前约会中的日期时间结束日期)
{
如果(IsAppPointmentTimeValid(endDate)==true)
{
有效时间。添加(结束日期);
}
}
}
private bool IsAppointmentTimeValid(DateTime newAppointmentEndTime)
{
//比较max(maxendDateTime)和newAppointmentDateTime
返回ValidEndTime.Max(t=>t.Date)
尝试获取当天的最长(结束时间),然后在最长(结束时间)之后开始任何其他约会。我没有真正理解你的意思。你能发布一个答案吗?我更改了答案,请检查一下,知道吗。
test: 9h00 <= 9h00 && 11h <= 10h00 (false)
private List<DateTime> ValidEndTime = new List<DateTime>(); //Contains the list of validEndDate
private void button1_Click(object sender, EventArgs e)
{
List<DateTime> currentAppointment = new List<DateTime>();
currentAppointment.Add(Convert.ToDateTime("22 May 2014 14:22"));
currentAppointment.Add(Convert.ToDateTime("22 May 2014 14:30"));
foreach (DateTime endDate in currentAppointment)
{
if (IsAppointmentTimeValid(endDate) == true)
{
ValidEndTime.Add(endDate);
}
}
}
private bool IsAppointmentTimeValid(DateTime newAppointmentEndTime)
{
//compare max(maxendDateTime) with the newAppointmentDateTime
return ValidEndTime.Max(t => t.Date) < newAppointmentEndTime? true : false;
}