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 和溢出异常?
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
