2

我有这个简单的 LINQ 查询:

Dim sourceSect = (From sect In allSections
                  Where sect.ORDER = sourceNode.Index
                  Select sect).Single()

sourceSect.ORDER = targetNode.Index

但是,如果我在线编写它:

(From sect in allSections Where sect.ORDER = sourceNode.index Select sect).Single().ORDER = targetNode.Index

我从 Visual Studio 收到语法错误。

有什么合理的理由吗?:)

在此处输入图像描述

4

2 回答 2

3

作为已接受答案的旁注。可以使用方法语法:

allSections.Single(Function(s) s.ORDER = sourceNode.Index).Order = targetNode.Index
于 2012-08-23T13:52:36.647 回答
2

你不能只写

(From sect in allSections Where sect.ORDER = sourceNode.index Select sect).Single().ORDER = targetNode.Index

在 VB.Net 中。使用查询语法时,必须先将结果分配给变量,然后才能设置属性

Dim sect = (From sect in allSections Where sect.ORDER = sourceNode.index Select sect).Single()
sect.ORDER = targetNode.Index

因此,您必须使用您的代码,或使用方法语法(正如 Tim Schmelter 指出的那样):

allSections.Single(Function(sect) sect.ORDER = sourceNode.Index).Order = targetNode.Index
于 2012-08-23T13:41:01.760 回答