LINQ to Dataset DBNULL 问题/空引用异常

发布于 2024-07-21 03:40:15 字数 1418 浏览 4 评论 0原文

我有以下 LINQ 查询,当 dtblDetail 中的“Remark”列为 null 时,它总是会导致错误,即使我测试它是否为 NULL。

var varActiveAndUsedElementsWithDetails =
                        from e in dtblElements
                        join d in dtblDetails on e.PK equals d.FK into set
                        from d in set.DefaultIfEmpty()
                        where (e.ElementActive == true)
                        select new
                        {
                            ElementPK = e.PK,
                            Remark = d.IsRemarkNull() ? null : d.Remark
                        };

错误消息是: “表‘dtblDetails’中‘备注’列的值为 DBNull。” 添加 d.IsRemarkNull() 测试后,会引发空引用异常。

你能帮我解决这个问题吗?

我已经检查了以下网站,但除了我必须测试 DBNULL 之外没有发现任何有用的东西。 但正如我所说,这并不能解决我的问题。

I have the following LINQ query which always results in an error when my "Remark" column in dtblDetail is null, even though I test if it is NULL.

var varActiveAndUsedElementsWithDetails =
                        from e in dtblElements
                        join d in dtblDetails on e.PK equals d.FK into set
                        from d in set.DefaultIfEmpty()
                        where (e.ElementActive == true)
                        select new
                        {
                            ElementPK = e.PK,
                            Remark = d.IsRemarkNull() ? null : d.Remark
                        };

The error message was:
"The value for column 'Remark' in table 'dtblDetails' is DBNull."
After adding the test for d.IsRemarkNull() a null reference exception is thrown.

Can you help me with this?

I've already checked the following websites but didn't find anything useful other than that I have to test for DBNULL. But as said this doesn't solve my problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳男子 2024-07-28 03:40:15

问题是整个“d”项是空的。
因此调用“d.IsRemarkNull()”会导致空引用异常。
以下代码解决了该问题:

var varActiveAndUsedElementsWithDetails =
                    from e in dtblElements
                    join d in dtblDetails on e.PK equals d.FK into set
                    from d in set.DefaultIfEmpty()
                    where (e.ElementActive == true)
                    select new
                    {
                        ElementPK = e.PK,
                        Remark = d == null? null : (d.IsRemarkNull() ? null : d.Remark)
                    };

The problem was that the whole 'd' item was empty.
So calling 'd.IsRemarkNull()' resulted in the null reference exception.
The following code fixed the problem:

var varActiveAndUsedElementsWithDetails =
                    from e in dtblElements
                    join d in dtblDetails on e.PK equals d.FK into set
                    from d in set.DefaultIfEmpty()
                    where (e.ElementActive == true)
                    select new
                    {
                        ElementPK = e.PK,
                        Remark = d == null? null : (d.IsRemarkNull() ? null : d.Remark)
                    };
﹉夏雨初晴づ 2024-07-28 03:40:15

错误从何而来? 是否有可能是 d.IsRemarkNull() 导致的? 该方法是什么样的?

也许:

DBNull.Value.Equals(d.Remark)

Where is the error coming from? Is it possible that it is the d.IsRemarkNull() that is causing it? What does that method look like?

Perhaps:

DBNull.Value.Equals(d.Remark)
暮倦 2024-07-28 03:40:15

也许这个字段不允许在数据库中为空,为它获取默认值,并避免处理空值

maybe this field doesn't allow null in db, get a default value for it, and avoid handling null values

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文