VB.Net Linq查询中的变量可以用作方法调用/ byref参数吗?

时间:2016-02-17 13:11:25

标签: vb.net linq

我刚刚在我的代码中发现了一个错误,因为我认为在调用查询中的方法时,当用作out(byref)参数时,Linq查询中声明的变量(通过Let关键字)不会被设置。
有谁知道这只对VB.Net有效还是对C#有效? 这是在某处记录的吗?

Let关键字的MSDN文档说明:

  

计算一个值并将其分配给查询中的新变量。

稍后的文档说明:

  

使用Let子句可以计算每个查询结果的值,并使用别名引用它们。

以下是我的代码失败的示例:

From c In Me.AllContractsInHourBlock
Let isForcedFullTrading As Boolean = False
Let noTradingReason As ContractNoTradingReason = ContractNoTradingReason.None
Let targetMode = c.CalculateTradingMode(instant, sendWarningMessageOnNoPrognosis, isForcedFullTrading, noTradingReason)
Select New With {
   .CurrentMode = c.TradingMode,
   .TargetMode = targetMode,
   .TradingModeChanged = (c.TradingMode <> targetMode),
   .IsForcedFullTrading = isForcedFullTrading,
   .CurrentNoTradingReason = c.NoTradingReason,
   .NoTradingReason = noTradingReason,
   .NoTradingReasonChanged = (targetMode = ContractTradingMode.NoTrading AndAlso c.NoTradingReason <> noTradingReason

在上面的代码示例中,变量noTradingReason和isForcedFullTrading将始终在达到Select New时最初分配。
如果我在Linq之外提取变量,那么一切正常。

Dim isForcedFullTrading As Boolean = False
Dim noTradingReason As ContractNoTradingReason = ContractNoTradingReason.None      
From c In Me.AllContractsInHourBlock
Let targetMode = c.CalculateTradingMode(instant, sendWarningMessageOnNoPrognosis, isForcedFullTrading, noTradingReason)
Select New With {
   .CurrentMode = c.TradingMode,
   .TargetMode = targetMode,
   .TradingModeChanged = (c.TradingMode <> targetMode),
   .IsForcedFullTrading = isForcedFullTrading,
   .CurrentNoTradingReason = c.NoTradingReason,
   .NoTradingReason = noTradingReason,
   .NoTradingReasonChanged = (targetMode = ContractTradingMode.NoTrading AndAlso c.NoTradingReason <> noTradingReason   

这是CalculateTradingMode方法的签名:

Function CalculateTradingMode(instant As DateTimeOffset, sendWarningMessageOnNoPrognosis As Boolean, ByRef isForcedFullTrading As Boolean, ByRef noTradingReason As ContractNoTradingReason) As ContractTradingMode

0 个答案:

没有答案