C# Linq where column ==(空引用)与 column == null 不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2097539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq where column == (null reference) not the same as column == null
提问by Boog
I came across a rather strange problem with linq-to-sql. In the following example,
我遇到了 linq-to-sql 一个相当奇怪的问题。在下面的例子中,
var survey = (from s in dbContext.crmc_Surveys
where (s.crmc_Retail_Trade_Id == tradeId) && (s.State_.Equals(state))
select s).First();
If tradeId is null, it doesn't behave as if I had specified null specifically like this instead,
如果 tradeId 为 null,则它不会表现得好像我已经像这样专门指定了 null,
var survey = (from s in dbContext.crmc_Surveys
where (s.crmc_Retail_Trade_Id == null) && (s.State_.Equals(state))
select s).First();
Which is my desired behavior. In fact it doesn't return anything unless both values are non-null. I can't figure out how to accomplish this short of several different linq queries. Any ideas?
这是我想要的行为。事实上,除非两个值都不为空,否则它不会返回任何内容。我不知道如何完成几个不同的 linq 查询。有任何想法吗?
采纳答案by jball
Change where (s.crmc_Retail_Trade_Id == tradeId)
to
更改where (s.crmc_Retail_Trade_Id == tradeId)
为
where (s.crmc_Retail_Trade_Id == tradeId ||
(tradeId == null && s.crmc_Retail_Trade_Id == null))
Edit - based on this postby Brant Lamborn, it looks like the following would do what you want:
编辑 - 基于Brant Lamborn 的这篇文章,看起来以下内容可以满足您的需求:
where (object.Equals(s.crmc_Retail_Trade_Id, tradeId))
The Null Semantics (LINQ to SQL)MSDN page links to some interesting info:
该null语义(LINQ到SQL)MSDN页面链接到一些有趣的信息:
LINQ to SQL does not impose C# null or Visual Basic nothing comparison semantics on SQL. Comparison operators are syntactically translated to their SQL equivalents. The semantics reflect SQL semantics as defined by server or connection settings. Two null values are considered unequal under default SQL Server settings (although you can change the settings to change the semantics). Regardless, LINQ to SQL does not consider server settings in query translation.
A comparison with the literal null (nothing) is translated to the appropriate SQL version (is null or is not null).
The value of null (nothing) in collation is defined by SQL Server; LINQ to SQL does not change the collation.
LINQ to SQL 不会对 SQL 强加 C# null 或 Visual Basic 无比较语义。比较运算符在语法上被转换为它们的 SQL 等价物。语义反映由服务器或连接设置定义的 SQL 语义。在默认 SQL Server 设置下,两个空值被视为不相等(尽管您可以更改设置以更改语义)。无论如何,LINQ to SQL 不会在查询转换中考虑服务器设置。
与文字 null(无)的比较被转换为适当的 SQL 版本(为 null 或不为 null)。
collation 中的 null(无)值由 SQL Server 定义;LINQ to SQL 不会更改排序规则。
回答by Joel Coehoorn
Not sure on this one, but I suspect when linq-to-sql translates that to an sql query string you get a slightly different expression specifying null
directly such that at some point you end up comparing NULL to itself, and NULL=NULL is defined to be false.
不确定这一点,但我怀疑当 linq-to-sql 将其转换为 sql 查询字符串时,您会得到一个略有不同的表达式,null
直接指定这样在某些时候您最终会将 NULL 与其自身进行比较,并且 NULL=NULL 被定义为假的。
回答by Duncan
I am not familiar with Linq, however in general:
我不熟悉 Linq,但总的来说:
NULL
represents a missing, unknown, or undefined value. Strictly speaking, a variable cannot equal NULL
; low-lvel languages which provide this construct usually do so as a convenience because there is no easy alternative -- at a higher level it's usually better to rely on ISNULL
, defined
, or whatever features your language supplies.
NULL
表示缺失的、未知的或未定义的值。严格来说,变量不能等于NULL
;提供这种结构的低级语言通常这样做是为了方便,因为没有简单的替代方案 - 在更高级别上ISNULL
,通常更好地依赖defined
、 或您的语言提供的任何功能。
One undefined variable is not equal to another undefined variable (and the same applies to NULL == NULL). Joe Celko has a good example of writing a query to find all people whose hair colour matches the colour of the vehicle they drive. Should this query match a bald man who walks everywhere?
一个未定义的变量不等于另一个未定义的变量(同样适用于 NULL == NULL)。Joe Celko 有一个很好的例子,编写一个查询来查找所有头发颜色与他们驾驶的车辆颜色相匹配的人。这个查询应该匹配一个到处走的秃头吗?
回答by Greg Klaus
Another option to solve this, as I ran across this problem as well.
解决这个问题的另一种选择,因为我也遇到了这个问题。
where (tradeId == null ? s.crmc_Retail_Trade_Id == null : s.crmc_Retail_Trade_Id == tradeId)
回答by Ubaid Ur Rehman
It's better to make sp for this purpose because linq will perform iteration it takes while for your assistance if you are using linq.
最好为此目的制作 sp,因为如果您使用 linq,linq 将执行迭代所需的时间来为您提供帮助。
var c = lstQ_Buffer.Where(q => (((semesterId == 0 || semesterId == null ? q.fkSemesterId == null : q.fkSemesterId == semesterId) && (subjectSpecialtyId == 0 || subjectSpecialtyId == null ? q.fkSubSpecialtyId == null : q.fkSubSpecialtyId == subSpecialtyId) && (subTopicId == 0 || subTopicId == null ? q.fkSubTopicId == null : q.fkSubTopicId == subTopicId)) && (q.fkProgramId == programId && q.fkYearId == yearId && q.fkCourse_ModuleId == courseModuleId && q.fkSubject_SpecialtyId == subjectSpecialtyId && q.fkTopicId == topicId && q.fkDifficultyLevelId == diffucultyLevelId))).ToList();