【发布时间】:2015-02-10 01:29:23
【问题描述】:
我被我希望是一个简单的问题难住了。我正在尝试嵌入一个简单的公式,增加一个变量以充当 XML 文档的“行号”。在 Visual Basic 中编写文字 XML。代码如下所示:
<%= From d In orderData
Select <ItemOut quantity=<%= d.OrderQuantity %> lineNumber=<%= i %>>
<ItemID>
<SupplierPartID><%= d.VendorPartNo %></SupplierPartID>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="USD"><%= d.PricePerPackage %></Money>
</UnitPrice>
<Description xml:lang="en"><%= d.Description %></Description>
<UnitOfMeasure><%= d.OrderUOM %></UnitOfMeasure>
</ItemDetail>
<%= i = i + 1 %>
</ItemOut>
%>
我希望 OrderData 中的 d 的每次迭代都勾选 i + 1,但是,它只是返回“false”。在此处查看输出 XML:
<ItemOut quantity="1" lineNumber="1">
<ItemID>
<SupplierPartID>99999</SupplierPartID>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="USD">0.00</Money>
</UnitPrice>
<Description xml:lang="en">Tub and Tile Caulk Biscuit</Description>
<UnitOfMeasure>cs</UnitOfMeasure>
</ItemDetail>false</ItemOut>
<ItemOut quantity="1" lineNumber="1">
<ItemID>
<SupplierPartID>999999</SupplierPartID>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="USD">0.00</Money>
</UnitPrice>
<Description xml:lang="en">Tub and Tile Caulk Almond</Description>
<UnitOfMeasure>cs</UnitOfMeasure>
</ItemDetail>false</ItemOut>
有可能做这种事情吗?我什至尝试调用一个函数:
lineNumber=<%= incrementI(i) %>>
但这也会导致输出为“false”。我在这里想念什么? 提前感谢您的帮助!
Visual Studio 2013
编辑—— 这是我所指的功能:
Private Function incrementI(i As Integer)
Return i = i + 1
End Function
【问题讨论】:
-
这就是我对内联版本的期望;请记住,VB 对 assignment 和 comparison 都使用
=,并且使用哪个取决于它是 statement 还是 的上下文i>expression - 因为它试图形成一个表达式,所以它比较i和i+1并得出结论它们不相等。没有看到你是如何定义函数的,就不能说函数一。 -
私有函数 incrementI(i As Integer) Return i = i + 1 End Function
-
是的,所以您再次在需要 表达式 的上下文中使用它,因此它将执行比较。如果您定义了函数的返回类型,编译器会帮助解决这个问题。而且,因为参数不是通过引用传递的,所以它也不会产生任何副作用(所以外部代码中的
i保留了它在调用函数之前的任何值)。
标签: xml vb.net visual-studio linq-to-xml xml-literals