LINQ to SQL - where 子句中的可为空类型 [英] LINQ to SQL - nullable types in where clause

查看:31
本文介绍了LINQ to SQL - where 子句中的可为空类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含空值列的表...当我尝试查询该列为空的记录时:

I have a table with a column that has null values... when I try to query for records where that column IS NULL:

这个作品:



        var list = from mt in db.MY_TABLE
                   where mt.PARENT_KEY == null
                   select new { mt.NAME };

这不会:



        int? id = null;
        var list = from mt in db.MY_TABLE
                   where mt.PARENT_KEY == id
                   select new { mt.NAME };

为什么?

推荐答案

经过更多的谷歌搜索,我找到了答案:

after some more googling, I found the answer:

参考 #1

ref #2

int? id = null;
var list = from mt in db.MY_TABLE
           where object.Equals(mt.PARENT_KEY, id)  //use object.Equals for nullable field
           select new { mt.NAME };

此 LINQ 对 SQL 的呈现方式如下:

((mt.PARENT_KEY IS NULL) AND (@id IS NULL)) 
OR ((mt.PARENT_KEY IS NOT NULL) AND (@id IS NOT NULL) AND (mt.PARENT_KEY = @id))

这篇关于LINQ to SQL - where 子句中的可为空类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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