如何比较整数类型空值在.Net Entity framework 4.1中的Linq查询部分?
答案 0 :(得分:12)
如果int
为NULL
,您只能将int
与nullable
进行比较。如果没有,则int
的默认值为0
,永远不会为null
。
您可以像这样定义一个可以为null的int属性:
int? value { get; set; }
并按照以下方式检查:
if ( value != null )
{
int x = value.Value;
}
在Linq查询的where子句中,它将是
var result = from d in data
where d.Value != null
select d
答案 1 :(得分:7)
如果您要与null
值进行比较,则必须先将您的值与null
进行比较,因为有错误。
var field = from field in table
where (value == null ? field.property == null : field.property == value)
select field;