使用 linq 时出现 NullReferenceException

发布于 2024-12-14 20:38:00 字数 292 浏览 2 评论 0原文

当我从界面中检查某些复选框时,以下代码行会抛出 NullReferenceException 错误,但当我检查所有复选框时,不会发生异常。每个选中的复选框都会导致创建一个对象,并且该对象存储在同一类的数组中。

someObjects.OrderByDescending(obj => obj.numericProperty);

数组 - someObjects - 包含一些我想要排序的选中复选框中选定的对象。数组 someObjects 也与对象具有相同的类型。请问我该如何解决这个问题?谢谢。

The following line of code throws NullReferenceException error When I check some checkboxes from the interface, but when I check all the checkboxes the exception won't occur. Each checkbox checked will cause an object to be created, and the objects are stored in an array of the same class.

someObjects.OrderByDescending(obj => obj.numericProperty);

The array - someObjects - contains some selected objects from some of the checked checkboxes which I want to sort. The array someObjects is also of the same type with the objects. Please how do I fix this? Thanks.

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

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

发布评论

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

评论(1

川水往事 2024-12-21 20:38:00

您的问题不清楚,但听起来 someObjects 包含一些空引用。

最好将它们过滤掉:

var query = someObjects.Where(obj => obj != null)
                       .OrderByDescending(obj => obj.numericProperty);

或者,避免将它们放入数组中。目前还不清楚数组是如何构建的,但是其中的空引用对您来说有用吗?

Your question is unclear, but it sounds like someObjects contains some null references.

It's probably best to just filter them out:

var query = someObjects.Where(obj => obj != null)
                       .OrderByDescending(obj => obj.numericProperty);

Or alternatively, just avoid putting them in the array to start with. It's not really clear how the array's being built, but is it useful to you to have null references in there?

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