VB.Net Linq与实体为空比较-'是什么'或'=什么都不是? [英] VB.Net Linq to Entities Null Comparison - 'Is Nothing' or '= Nothing'?

查看:100
本文介绍了VB.Net Linq与实体为空比较-'是什么'或'=什么都不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在VB.Net中有多个项目,其中许多我们的SQL查询都使用.Net Framework 4和Linq to Entities.迁移到EF对我们来说是一个新的转变(已经使用了大约4-6个月),并且得到了高层管理人员的支持,因为我们可以更快地进行编码.我们仍然使用大量存储的proc,但是我们甚至也通过Linq to Entities执行它们.

We have several projects in VB.Net, using .Net Framework 4 and Linq to Entities for many of our SQL queries. Moving to EF is a new shift for us (been using it for about 4-6 months) and has the backing of upper management because we can code so much faster. We still use a lot of stored procs, but we even execute those through Linq to Entities as well.

我希望能消除一些困惑,但是我找不到一个合理的直接答案.我们有一些查询,我们想要在其中记录特定字段具有NULL值的地方.这些是简单的选择查询,没有聚合或左联接,等等.Microsoft建议查询看起来像这样

I'm hoping to clear some confusion up and I can't find a direct answer that makes sense. We have some queries where we want records where a specific field has a NULL value. These are simple select queries, no aggregates or left joins, etc. Microsoft recommends the query look something like this MSDN Link:

dim query = from a in MyContext.MyTables
Where a.MyField = Nothing
Select a

我有几个项目可以完全做到这一点,而且效果很好,IDE中没有警告.最近,另一个开发人员创建了一个新项目,当他像上面那样进行空检查时,我们所有人都在IDE中收到以下警告:

I have several projects where I do exactly this and it works great, no warnings in the IDE. Recently a new project was created by another developer and when he did his null check like above, we all get this warning in the IDE:

警告1该表达式将始终为Nothing(由于equals运算符的空传播).要检查该值是否为空,请考虑使用是什么".

Warning 1 This expression will always evaluate to Nothing (due to null propagation from the equals operator). To check if the value is null consider using 'Is Nothing'.

比较项目时,每个项目都启用了显式选项和严格选项.如果忽略警告,则将获得运行该应用程序时正在寻找的确切记录集.如果将=符号更改为IS,则警告消失.但是,为什么这个警告出现在一个项目中而不是其他项目中?即使在MSDN上也有使用equals运算符的示例,这令人困惑.

Comparing the projects, option explicit and option strict are on for each one. If we ignore the warning, we get the exact record set we are looking for when the app runs. The warning goes away if I change the = sign to IS. But why did this warning appear in one project and not others? It's confusing when even on MSDN there are examples using the equals operator.

推荐答案

我相信您在这里看到的是MyFieldNullable(Of T)类型.可能是原始IntegerSingle等...

I believe what you're seeing here is that MyField is a Nullable(Of T) type. Likely a primitive Integer, Single, etc ...

看到此警告的原因是因为编译器将原始类型的普通相等运算符提升为Nullable(Of T)版本.它实质上执行以下操作

The reason you're seeing this warning is because the compiler promotes the normal equality operator for the primitive type to the Nullable(Of T) version. It essentially executes the following

Dim myField As Integer? = a.MyField
Dim other As Integer? = Nothing
If myField = other Then
 ...
End If

问题是,当Integer?的值为Nothing时,它将不等于任何东西.因此,上述Where子句将始终返回False.编译器试图警告您有关Nullable(Of T)的问题,并将您推向Is Nothing检查,这将确定a.MyField是否具有非空值.

The issue though is that when Integer? has the value Nothing it won't compare equal to anything. Hence the above Where clause will always return False. The compiler is attempting to warn you about this problematic corner of Nullable(Of T) and push you to a Is Nothing check which will determine if a.MyField has a non-null value.

此博客文章非常详细地说明了为什么会生成此警告及其背后的所有机制.本文是针对C#编写的,但基本前提也适用于VB.Net.

This blog article has a very detailed explanation of why this warning is being generated and all of the mechanics behind it. The article is written for C# but the basic premise is applicable to VB.Net as well.

这篇关于VB.Net Linq与实体为空比较-'是什么'或'=什么都不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆