VB->选项严格和LINQ结果

时间:2014-02-17 14:15:16

标签: vb.net linq option-strict

我最近介绍了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查询转换为另一种类型不起作用?这样做的正确方法是什么,并保持选项严格开启

0 个答案:

没有答案