Linq == null vs IsNullOrEmpty - 不同或相同?

Key*_*dly 0 c# linq linq-to-sql

LinqLinq to Sql更准确:是那里的之间的差异== nullIsNullOrEmpty在下面的查询?

From a in context.SomeTable
where a.SomeId.Equals(SomeOtherId)
&& a.SomeOtherColumn == null
Select new .....
Run Code Online (Sandbox Code Playgroud)

&

From a in context.SomeTable
where a.SomeId.Equals(SomeOtherId)
&& string.IsNullOrEmpty(a.SomeOtherColumn)
Select new .....
Run Code Online (Sandbox Code Playgroud)

Dav*_*haw 5

你不能String.IsNullOrEmpty在Linq-SQL中做:

方法'Boolean IsNullOrEmpty(System.String)'没有支持的SQL转换.

如果你需要,我想你必须在where子句中同时进行null和empty检查:

&& (a.SomeOtherColumn == null || a.SomeOtherColumn == "")
Run Code Online (Sandbox Code Playgroud)

这将转换为SQL中的空和空检查.

查看其他答案,使用.Length == 0,将生成SQL以检查varchar列的长度,这可能不如检查varchar是否等于''.

编辑:这是关于SQL的长度与空检查的Stack Overflow 答案.我猜对了.