私はこの例の修正に取り組んでいます:
Using advWorksContext As New AdventureWorksEntities
' Call the constructor that takes a command string and ObjectContext.
Dim productQuery1 As New ObjectQuery(Of Product)("Product", advWorksContext)
Dim result As Product
For Each result In productQuery1
Console.WriteLine("Product Name: {0}", result.Name)
Next
End Using
ObjectQuery は 1 回だけ番号付けできます。(以降の列挙の試みは例外をスローします。) 列挙は for each ステートメントで行われます。問題は、クエリが空の場合に For Each を試行すると例外がスローされることです。しかし、カウントを確認すると:
If productQuery1.Count > 0 Then . . .
それは列挙の私の 1 つのチャンスを食べます。For Each を try/catch ブロックに入れ子にして、空のクエリ例外を破棄することもできますが、それは見苦しいものです。より良い解決策はありますか?