我最近被介绍到 LINQ,并获得了一个示例,如何在对数据库执行查询后搜索错误消息。代码如下所示:
Dim errors As Object = From e In execution.Messages _
Where e.MessageType = 120 Or e.MessageType = 110
Select e.Message
If errors IsNot Nothing Then
For Each m As OperationMessage In errors
LogToError("ExecutePackage->Error message: " & m.Message)
sbError.AppendLine(m.Message)
Next
End If
现在,当我使用Option strict ON时,我在这一行的“错误”对象上得到一个错误:
For Each m As OperationMessage In errors
这对我来说很自然。因此,我尝试将代码更改为:
For Each m As OperationMessage In CType(errors, OperationMessageCollection)
现在,当我运行它时,我得到了这个错误:
Unable to cast object of type 'WhereSelectEnumerableIterator`2[Microsoft.SqlServer.Management.IntegrationServices.OperationMessage,System.String]' to type 'Microsoft.SqlServer.Management.IntegrationServices.OperationMessageCollection'.
那么,在我看来,在运行时将 LINQ 查询转换为另一种类型不起作用?这样做并保持Option strict ON的正确方法是什么?