LINQ 在 VB.NET 中进行短路评估吗?
今天,我们在这行代码上遇到了 Nullable object must have a value
错误:
list = From x In Me Where x.FooDate.HasValue AndAlso x.FooDate.Value.Date >= barDate
有趣的是,我确信这曾经工作得很好(并且底层中总是有一些空值)数据)。从逻辑上讲,它对我来说看起来不错。对 HasValue
和 AndAlso
的检查看起来好像它们会保护我们免受任何 Null 危险。
但突然间他们似乎就不是了。我错过了什么吗?
好的,我们可以更正它,从而消除错误:
list = From x In Me Where If(x.FooDate.HasValue, x.FooDate.Value.Date >= barDate,False)
但这在我看来不太可读。有什么想法吗?
更新...和忏悔:
在简化上面的代码以缩短代码行时,我遗漏了代码的关键部分。最初的问题应该是这样的:
list = From x In Me Where x.FooDate.HasValue AndAlso x.FooDate.Value.Date >= fromDate And x.FooDate.Value.Date <= toDate
因为短路和运算符优先级的规则(如在答案中概述很久以前我自己的问题)我需要在指令的第二部分周围添加括号,以便停止 LINQ 评估第二个 x.FooDate.Value.Date
:
list = From x In Me Where x.FooDate.HasValue AndAlso (x.FooDate.Value.Date >= fromDate and x.FooDate.Value.Date <= toDate)
感谢两个答案的抛出快速测试代码来验证 LINQ 确实遵守 AndAlso 并迫使我更仔细地查看原始问题。
We had a Nullable object must have a value
error today on this line of code:
list = From x In Me Where x.FooDate.HasValue AndAlso x.FooDate.Value.Date >= barDate
Funny thing is that I feel sure this used to work fine (and there has always been a sprinkling of null values in the underlying data). And logically it looks fine to me. The check for HasValue
and the AndAlso
look as if they're going to shield us from any Null danger.
But it seems suddenly they're not. Am I missing something?
OK, we can correct it to this, which eliminates the error:
list = From x In Me Where If(x.FooDate.HasValue, x.FooDate.Value.Date >= barDate,False)
But this looks to me less readable. Any thoughts?
Update ... and Confession:
In simplifying the above code to shorten the line I left out a crucial chunk of the code. The original problem should have read something like:
list = From x In Me Where x.FooDate.HasValue AndAlso x.FooDate.Value.Date >= fromDate And x.FooDate.Value.Date <= toDate
Because of the rules of shortcircuiting and operator precedence (as outlined in an answer to a long ago question of my own) I needed to add brackets round the second part of the instruction in order to stop LINQ evaluating the second x.FooDate.Value.Date
:
list = From x In Me Where x.FooDate.HasValue AndAlso (x.FooDate.Value.Date >= fromDate and x.FooDate.Value.Date <= toDate)
Thanks to both answers for throwing up quick test code to verify that LINQ really does obey AndAlso and force me to look more closely at the original problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定您的查询是安全的,至少以下示例代码是:
注意:如果我将
AndAlso
替换为And
我会得到您的异常。所以问题一定出在其他地方。请向我们展示更多您的课程。
I'm pretty sure that your query is safe, at least following sample code is:
Note: If i would replace
AndAlso
withAnd
i would get your exception.So the problem must be somewhere else. Show us more of your class please.
它应该有效。我编写了这段代码,即使将
barDate
设置为Nothing
,它对我来说也很好用。It should work. I wrote this code, and it works for me fine, even with
barDate
set toNothing
.