Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq==null与IsNullOrEmpty——不同还是相同?_C#_Linq_Linq To Sql - Fatal编程技术网

C# Linq==null与IsNullOrEmpty——不同还是相同?

C# Linq==null与IsNullOrEmpty——不同还是相同?,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,在Linq或Linq to Sql中更准确地说:==null和IsNullOrEmpty在以下查询中是否存在差异 From a in context.SomeTable where a.SomeId.Equals(SomeOtherId) && a.SomeOtherColumn == null Select new ..... & 不能在Linq SQL中执行String.IsNullOrEmpty: 方法“Boolean IsNullOrEmpty(System.Strin

Linq
Linq to Sql
中更准确地说:
==null
IsNullOrEmpty
在以下查询中是否存在差异

From a in context.SomeTable
where a.SomeId.Equals(SomeOtherId)
&& a.SomeOtherColumn == null
Select new .....
&


不能在Linq SQL中执行
String.IsNullOrEmpty

方法“Boolean IsNullOrEmpty(System.String)”不受支持 转换为SQL

如果需要,我想您必须在where子句中同时使用null和empty检查:

&& (a.SomeOtherColumn == null || a.SomeOtherColumn == "")
这将在SQL中转换为null和空签入

查看其他答案,使用
.Length==0
,将生成SQL来检查varchar列的长度,这可能比检查varchar是否等于
'
效率低


编辑:这里有一个堆栈长度溢出,而不是SQL的空检查。看来我猜对了。

string.IsNullOrEmpty也适用于空字符串,如
最明显的区别在于名称。还检查字符串是否为空。这相当于:

from a in context.SomeTable
where a.SomeId.Equals(SomeOtherId)
&& (a.SomeOtherColumn == null || a.SomeOtherColumn.Length == 0)
...


IsNullOrEmpty
等于

s == null || s.Length == 0;

s
string

的实例时,其他答案说明了一个明显的事实,
.IsNullOrEmpty()
检查空字符串,同样重要的是要注意比较

where a.SomeOtherColumn == someNullVariable

由于使用SQL空比较而不是C#空比较,将永远不会返回LINQ to SQL中的任何值s == null || s.Length == 0;
where a.SomeOtherColumn == someNullVariable