Linq orderby 子句中发生 InvalidOperationException

发布于 2024-12-08 14:53:42 字数 1196 浏览 0 评论 0原文

这是我正在使用的 linq 语句:

var sortedList =
        (from p in OriginalList
         where p.NValue != null
         orderby Math.Abs(p.NValue.Value) descending
         select p);

OriginalList 是包含超过 10,000 个元素的 Transaction 对象的列表。 NValue 是 Transaction 的可空属性。每次更新 OriginalList 时,都会执行该语句。

我发现这个语句有时会抛出以下异常: System.InvalidOperationException:可为 Null 的对象必须有一个值。

我尝试进行单元测试,并为其提供只有一个事务的 OriginalList。该交易的 NValue 为空。不会触发这个异常。

有人知道这里发生了什么事吗?多谢。

我们正在使用 Linq to SQL。这是堆栈跟踪:

2011-10-05 16:14:06,826 [SRV101 DC\Admin] [59] ERROR Utils.AProxy`1 - AProxy [TProxy] 加载期间错误

System.InvalidOperationException: Nullable object must have a value.

at CServer.TLoader.b__2(Trasaction p) in c:\...\TLoader.cs:line 61
at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)
at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
at System.Linq.OrderedEnumerable`1.d__0.MoveNext()
at CServer.TLoader.GetMultipliers(IEnumerable`1 OriginalList) in c:\...\TLoader.cs:line 64
at CServer.TProxy.OnLoad() in c:\...\TProxy.cs:line 29
at Utils.AProxy`1.Load() in c:\...\AProxy.cs:line 252

Here is the linq statement I am using:

var sortedList =
        (from p in OriginalList
         where p.NValue != null
         orderby Math.Abs(p.NValue.Value) descending
         select p);

OriginalList is a list of Transaction objects with more than 10 thousand elements. NValue is nullable property of Transaction. Every time OriginalList is updated, the statement will be executed.

I have found that from time to time, this statement could throw the following exception:
System.InvalidOperationException: Nullable object must have a value.

I tried to do unit testing and fed it with an OriginalList with only one Transaction. This Transaction has null NValue. It won't trigger this exception.

Anyone has idea what is going on here? Thanks a lot.

We are using Linq to SQL. Here is the stack trace:

2011-10-05 16:14:06,826 [SRV101 DC\Admin] [59] ERROR Utils.AProxy`1 - AProxy [TProxy] error during load

System.InvalidOperationException: Nullable object must have a value.

at CServer.TLoader.b__2(Trasaction p) in c:\...\TLoader.cs:line 61
at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)
at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
at System.Linq.OrderedEnumerable`1.d__0.MoveNext()
at CServer.TLoader.GetMultipliers(IEnumerable`1 OriginalList) in c:\...\TLoader.cs:line 64
at CServer.TProxy.OnLoad() in c:\...\TProxy.cs:line 29
at Utils.AProxy`1.Load() in c:\...\AProxy.cs:line 252

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

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

发布评论

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

评论(2

携余温的黄昏 2024-12-15 14:53:42

您看到的错误似乎表明您正在尝试评估 Null 上的 .Value 。尝试使用 .GetValueOrDefault 代替,否则更改 where 子句来检查 HasValue 评分者,而不仅仅是与 null 进行比较:

var sortedList h= 
        (from p in OriginalList 
         where p.NValue.HasValue 
         orderby Math.Abs(p.NValue.Value) descending 
         select p); 

在您的情况下 p.NValue = Nullable 因此 p.NValue == null evalues 为 True,但 p.NValue.HasValue == false 。

The error you are seeing seems to indicate that you are trying to evalute .Value on a Null. Try using .GetValueOrDefault instead, otherwise change your where clause to check for HasValue rater than just comparing against null:

var sortedList h= 
        (from p in OriginalList 
         where p.NValue.HasValue 
         orderby Math.Abs(p.NValue.Value) descending 
         select p); 

In your case p.NValue = Nullable thus p.NValue == null evalues to True, but p.NValue.HasValue == false.

挽清梦 2024-12-15 14:53:42

我认为 Linq-to-Sql 不支持 Math.Abs​​,所以你可以这样做

var sortedList =
        (from p in OriginalList
         where p.NValue != null
         orderby p.NValue.Value < 0 ? (p.NValue.Value * -1) : p.NValue.Value descending
         select p);

I don't think Math.Abs is supported in Linq-to-Sql, so you can do something like this

var sortedList =
        (from p in OriginalList
         where p.NValue != null
         orderby p.NValue.Value < 0 ? (p.NValue.Value * -1) : p.NValue.Value descending
         select p);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文