我正在制定一個時間表的一段代碼。我使用的大學模塊和相關於這些模塊事件的系統,即比較重疊的日期 - 不避免
模塊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