Linq orderby 子句中发生 InvalidOperationException
这是我正在使用的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您看到的错误似乎表明您正在尝试评估 Null 上的 .Value 。尝试使用 .GetValueOrDefault 代替,否则更改 where 子句来检查 HasValue 评分者,而不仅仅是与 null 进行比较:
在您的情况下 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:
In your case p.NValue = Nullable thus p.NValue == null evalues to True, but p.NValue.HasValue == false.
我认为 Linq-to-Sql 不支持 Math.Abs,所以你可以这样做
I don't think Math.Abs is supported in Linq-to-Sql, so you can do something like this