我有一個結構數組(aryShooters())。我需要確定他們在哪個職位以及他們是否活躍。如果他們是那麼我想存儲他們在哪個索引。如果帖子未被填充,那麼數組中該位置的索引被設置爲-1。我遇到的問題是,在我的查詢中,我檢查它,然後使用AndAlso,不會將其餘線路短路。而且還不能在Linq查詢vb.net
這裏是我設定的指標:
Dim p1i as integer = -1
Dim p1i as integer = -1
Dim p1i as integer = -1
Dim p1i as integer = -1
Dim p1i as integer = -1
For j as integer = 0 to UBound(aryShooters)
If aryShooters(j).active = true Then
Select Case aryShooters(j).post
Case 1
p1i = j
Case 2
p2i = j
Case 3
p3i = j
Case 4
p4i = j
Case 5
p5i = j
End Select
End If
Next
並且之後我打電話給我的查詢。它只是最後5條線,這是搞砸了。
Dim pos
pos = From p In db.Banks
Where p.ShootId = shootIdProp And p.EventId = eventNo _
And ((p.FlightNo >= (CurFlight - kd)) And (p.FlightNo <= (CurFlight + (x-1) + ku))) _
And ((p.SectionNo Like lowsec) Or (p.SectionNo like upsec)) _
And p.Inactive = False And (((p.P1 like p1a And p.P2 Like p2a And p.P3 Like p3a And p.P4 Like p4a And p.P5 Like p5a) And resetFlags = False) _
Or ((p.NmbrOpenPosts >= numberShooters) And resetflags = True)) _
And ((((p1i > -1) AndAlso ((Math.Abs(aryShooter(p1i).Ydg - p.HcpYds1) <= perTrap) Or (p.HcpYds1 = 0.0))) And _
(((p2i > -1) AndAlso ((Math.Abs(aryShooter(p2i).Ydg - p.HcpYds2) <= perTrap) Or (p.HcpYds2 = 0.0))) And _
(((p3i > -1) AndAlso ((Math.Abs(aryShooter(p3i).Ydg - p.HcpYds3) <= perTrap) Or (p.HcpYds3 = 0.0))) And _
(((p4i > -1) AndAlso ((Math.Abs(aryShooter(p4i).Ydg - p.HcpYds4) <= perTrap) Or (p.HcpYds4 = 0.0))) And _
(((p5i > -1) AndAlso ((Math.Abs(aryShooter(p5i).Ydg - p.HcpYds5) <= perTrap) Or (p.HcpYds5 = 0.0))))
Select p
編輯 - 嗯,我修好了。只是在位置0添加了一個空白點。它仍然可以引用它,但從未實際讀過它。
你爲什麼在這裏使用位操作呢? – Magnus
@Magnus,你會怎麼建議儘可能有效地在一個有很多標準的查詢中選擇?不會在每個循環中通過一個巨大的if語句運行標準的效率低於通過Linq/Sql來運行它,這意味着要通過表進行排序? –
你確定你不應該在這裏到處使用'AndAlso'和'OrElse'嗎? – Magnus