LINQ Sum OverflowException?(LINQ 和溢出异常?)
问题描述
我已经为 EventLogEntry 实现了一个自定义的 IEqualityComparer.
I've implemented a custom IEqualityComparer for EventLogEntry.
public class EventLogEntryListComparison :
IEqualityComparer<List<EventLogEntry>>,
IEqualityComparer<EventLogEntry>
对于IEqualityComparer
,GetHashCode函数很简单.>
For IEqualityComparer<List<EventLogEntry>>
, the GetHashCode function is very simple.
public int GetHashCode(List<EventLogEntry> obj)
{
return obj.Sum(entry => 23 * GetHashCode(entry));
}
但是,对于某些条目,这会引发 OverflowException.
However, this throws an OverflowException for certain entries.
"Arithmetic operation resulted in an overflow."
at System.Linq.Enumerable.Sum(IEnumerable`1 source)
at System.Linq.Enumerable.Sum[TSource](IEnumerable`1 source, Func`2 selector)
at <snip>.Diagnostics.EventLogAnalysis.EventLogEntryListComparison.GetHashCode(List`1 obj) in C:dev<snip>Diagnostics.EventLogAnalysisEventLogEntryListComparison.cs:line 112
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)
at <snip>.Diagnostics.EventLogAnalysis.Program.AnalyseMachine(String validMachineName) in C:dev<snip>.Diagnostics.EventLogAnalysisProgram.cs:line 104
at System.Threading.Tasks.Parallel.<>c__DisplayClass2d`2.<ForEachWorker>b__23(Int32 i)
at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()
在调试时尝试得到相同的错误并且无法在即时窗口中出现后,我将代码更改为这个并再见 OverflowException?
After trying to get the same error whilst debugging and couldn't in the immediate window, I changed the code to this and bye bye OverflowException?
int total = 0;
foreach (var eventLogEntry in obj)
{
total += GetHashCode(eventLogEntry);
}
return total;
LINQ 的 Sum 函数有何不同?
How is it that LINQ's Sum function behaving differently?
编辑 2
感谢一些评论,更正和预期的 GetHashCode 函数现在如下,
Thanks to a few comments, the corrected and intended GetHashCode function is now as follows,
public int GetHashCode(List<EventLogEntry> obj)
{
return unchecked(obj.Aggregate(17,
(accumulate, entry) =>
accumulate * 23 + GetHashCode(entry)));
}
推荐答案
LINQ 的 Enumerable.Sum(...)
方法在 checked
块内执行加法.这意味着如果总和溢出,他们会故意抛出异常.
LINQ's Enumerable.Sum(...)
methods perform the additions inside a checked
block. This means that they deliberately throw an exception if the sum overflows.
您的 sum 不在 checked
块内,因此它是否引发异常取决于...是从 checked
块内调用它,还是我相信在程序集上的一个属性.
Your sum is not inside a checked
block, so whether or not it throws an exception depends on... whether it is called from inside a checked
block, or a property on the assembly I believe.
这篇关于LINQ 和溢出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:LINQ 和溢出异常?


基础教程推荐
- 从 C# 控制相机设备 2022-01-01
- 如果条件可以为空 2022-01-01
- 将数据集转换为列表 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01