Why can I do the following :
Dim qNodes As IQueryable(Of XmlNode) = xDoc.ChildNodes.AsQueryable()
Dim test = qNodes.Where(Function(node) True)
although the following gives the error I stated in the title :
Dim qNodes As IQueryable(Of XmlNode) = xDoc.ChildNodes.AsQueryable()
Dim test = qNodes.Where(Function(node)
Return True
End Function)
?
I really don't get it.
This is stated in section 11.1 of the VB.NET 10 Language Specification:
It is the restriction in the first item that you are running into. The introduction leaves plenty room to assume that this will be worked on in future releases, there's much to be gained of course. Connect.microsoft.com is a good place to go to encourage them. I couldn't check if a feedback item was already opened for this, the site is on the fritz right now.
VB.Net supports two kinds of lambda expressions:
Expression lambdas contain a single expression and implicitly return the expression.
For example:
Function(x) x.ToString()
Statement lambdas contain one or more statements and must explicitly use the
Return
keyword (if they return a value)These are new to VB.Net 2010.
For example:
The
Where
method, and all otherIQueryable
methods, take expression trees.The compiler can automatically compile expression lambdas, but not statement lambdas, into expression trees.
Your second example tries to pass a statement lambda as an
Expression(Of Func(Of T, Boolean))
, but the compiler can't do that.