我正在制定一个时间表的一段代码。我使用的大学模块和相关于这些模块事件的系统,即比较重叠的日期 - 不避免
模块CSC3039 事件1 - 讲座 事件2 - 讲座 EVENT3 - Practial等
我需要检查每一个事件的时间在模块相互对抗并比较冲突。冲突不需要纠正,只是突出显示。我将使用的表格是Events
,其中包含Event_ID (PK), Module_code (FK), Start_Date_Time, End_Date_Time
加上其他无关紧要的字段。我已经想通了,我需要实现一个For Each
声明,最终导致if语句如:
如果(startTime1 < = endTime2或endTime1> = startTime2)CLASH
我的问题是试图找出这里的实际循环。我不知道该写什么来宣布我的开始时间和结束时间。我认为这是一个事件1并获取其开始和结束,然后检查事件2,3或4是否符合上述if语句的情况。我试图得到这个,但可以真正使用一些指导。
编辑...基于下面的建议我已经实现了下面的代码:
'return all relevant tables from the Modules database, based on the module code entered by the user.
Dim eventTime = (From mods In db.Modules
Join evnt In db.Events On mods.Module_code Equals evnt.Module_code
Join rm In db.Rooms On rm.Room_ID Equals evnt.Room_ID
Join build In db.Buildings On build.Building_code Equals rm.Building_code
Where ((mods.Module_code = initialModCode) And (evnt.Room_ID = rm.Room_ID))
Select evnt.Event_ID, evnt.Module_code, evnt.Event_type, evnt.Start_Date_Time, evnt.End_Date_Time, build.Building_code, rm.Room_Number)
'use the gridview to display the result returned by the above query
gdvEventsTable.DataSource = eventTime
gdvEventsTable.DataBind()
Dim listClashes As New List(Of Array)
For i As Integer = 0 To eventTime.Count - 1
For j As Integer = i + 1 To eventTime.Count - 1
If (eventTime.ToList(i).Start_Date_Time < eventTime.ToList(j).End_Date_Time) And (eventTime.ToList(i).End_Date_Time > eventTime.ToList(j).Start_Date_Time) Then
MsgBox("Clash", MsgBoxStyle.MsgBoxSetForeground, "")
listClashes.Add(eventTime)
Else
MsgBox("No Clash", MsgBoxStyle.MsgBoxSetForeground, "")
End If
Next
Next
当试图将事件添加到我的数组列表,我注意到,在调试,没有事件发送到名单。
好吧,现在这是越来越linq具体,我从来没有使用linq,所以我不知道这个东西。您需要以某种方式获取eventTime的第i个元素。在调试时将鼠标放在它上面,以查看eventTime是什么类型的对象,然后将它放到Google或其他位置。你也可以从gridview中获得所有的信息,这可能会更容易。 – 2013-04-28 20:09:59
你应该使用包容/排他的范围,而不是完全包含的范围。看到[我的回答](http://stackoverflow.com/a/16282246/634824)刚刚发布的类似问题。 – 2013-04-29 15:31:05