gpt4 book ai didi

.net - VB.NET : "Statement lambdas cannot be converted to expression trees" compile time error

转载 作者:行者123 更新时间:2023-12-04 05:21:20 28 4
gpt4 key购买 nike

为什么我要执行以下操作:

Dim qNodes As IQueryable(Of XmlNode) = xDoc.ChildNodes.AsQueryable()
Dim test = qNodes.Where(Function(node) True)

尽管以下给出了我在标题中指出的错误:
Dim qNodes As IQueryable(Of XmlNode) = xDoc.ChildNodes.AsQueryable()
Dim test = qNodes.Where(Function(node)
Return True
End Function)



我真的不明白。

最佳答案

VB.NET 10语言规范的11.1节对此进行了说明:

The exact translation between lambda methods and expression trees may not be fixed between versions of the compiler and is beyond the scope of this specification. For Microsoft Visual Basic 10.0, all lambda expressions may be converted to expression trees subject to the following restrictions:

  1. Only single-line lambda expressions without ByRef parameters may be converted to expression trees. Of the single-line Sub lambdas, only invocation statements may be converted to expression trees.
  2. Anonymous type expressions cannot be converted to expression trees if an earlier field initializer is used to initialize a subsequent field initializer, e.g. New With {.a=1, .b=.a}
  3. Object initializer expressions cannot be converted to expression trees if a member of the current object being initialized is used in one of the field initializers, e.g. New C1 With {.a=1, .b=.Method1()}
  4. Multi-dimensional array creation expressions can only be converted to expression trees if they declare their element type explicitly.
  5. Late-binding expressions cannot be converted to expression trees.
  6. When a variable or field is passed ByRef to an invocation expression but does not have exactly the same type as the ByRef parameter, or when a property is passed ByRef, normal VB semantics are that a copy of the argument is passed ByRef and its final value is then copied back into the variable or field or property. In expression trees, the copy-back does not happen.

All these restrictions apply to nested lambda expressions as well.



这是您遇到的第一项中的限制。引言留有足够的空间来假设它将在将来的版本中进行处理,当然会有很多收获。 Connect.microsoft.com是鼓励他们的好地方。我无法检查是否已为此打开反馈项目,该站点现在处于困惑状态。

关于.net - VB.NET : "Statement lambdas cannot be converted to expression trees" compile time error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5036549/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com