方法语法会阻止隐式转换,但查询语法不会。Option Strict
开启。使用查询语法时如何强制出现错误?
整个(完全可运行)程序:
Option Strict On
Module Module1
Sub Main()
Dim custList As New List(Of Cust)()
custList.Add(New Cust() With {.Name = "Mr. Current", .Deleted = False})
custList.Add(New Cust() With {.Name = "Mrs. Deleted", .Deleted = True})
custList.Add(New Cust() With {.Name = "Miss Null", .Deleted = Nothing})
Dim QuerySyntax =
From c In custList
Where c.Deleted = False 'no error (the problem)
Dim MethodSyntax =
custList _
.Where(Function(c) c.Deleted = False) 'compiler error (desired effect)
For Each c As Cust In QuerySyntax
Console.WriteLine("Q: " & c.Name & " " & c.Deleted)
Next
For Each c As Cust In MethodSyntax
Console.WriteLine("M: " & c.Name & " " & c.Deleted)
Next
Console.ReadKey(True)
End Sub
Class Cust
Public Property Name() As String
Public Property Deleted() As System.Nullable(Of Boolean)
End Class
End Module
问题的症结所在:
Where c.Deleted = False 'no error
.Where(Function(c) c.Deleted = False) 'compiler error