LINQ总和OverflowException异常? [英] LINQ Sum OverflowException?

查看:157
本文介绍了LINQ总和OverflowException异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了EventLogEntry一个自定义的IEqualityComparer

 公共类EventLogEntryListComparison:
的IEqualityComparer<名单< EventLogEntry> ;>中
的IEqualityComparer< EventLogEntry>

有关的IEqualityComparer<名单< EventLogEntry>> 的GetHashCode的功能很简单

 公众诠释的GetHashCode(列表< EventLogEntry> OBJ)
{
返回obj.Sum(入门= GT; 23 *的GetHashCode(入门));
}



不过,这将引发对某些项的发生OverflowException。

 算术运算导致溢出。 
在System.Linq.Enumerable.Sum(IEnumerable`1源)
在System.Linq.Enumerable.Sum [TSource](IEnumerable`1源,Func`2选择器)
AT< ;剪断> .Diagnostics.EventLogAnalysis.EventLogEntryListComparison.GetHashCode(List`1 OBJ)在C:\dev\<&剪断GT; Diagnostics.EventLogAnalysis\EventLogEntryListComparison.cs:行112
。在System.Collections.Generic .Dictionary`2.Insert(TKEY的关键,TValue值,布尔加)在System.Collections.Generic.Dictionary`2.set_Item
(TKEY的关键,TValue值)
AT<&剪断GT; .Diagnostics .EventLogAnalysis.Program.AnalyseMachine(字符串validMachineName)在C:\dev\<&剪断GT; .Diagnostics.EventLogAnalysis\Program.cs:行104
在System.Threading.Tasks.Parallel其中p;> ; c__DisplayClass2d`2&所述。ForEachWorker方式> b__23(的Int32ⅰ)
。在System.Threading.Tasks.Parallel&下;> c__DisplayClassf`1&所述。ForWorker> b__c()

试图让同样的错误,而调试和不能在即时窗口,我改变了代码,这个和再见OverflowException异常后?

  INT总= 0; 
的foreach(OBJ中VAR eventLogEntry)
{
总+ =的GetHashCode(eventLogEntry);
}

总回报;



它是如何LINQ的Sum函数表现不同?



编辑2



由于采用了一些意见,校正并打算GetHashCode的功能如下内容,

 公众诠释的GetHashCode(列表< EventLogEntry> OBJ)
{
返回选中(obj.Aggregate(17,
(累积,条目)= GT;
累加* 23 +的GetHashCode(条目)));
}


解决方案

LINQ的 Enumerable.Sum(...)方法执行检查块内的补充。这意味着,如果总和溢出,他们故意抛出异常。



您和是不是检查块内,所以不论它抛出一个异常要看...无论是从一个叫检查块,或者我相信大会的属性里面。


I've implemented a custom IEqualityComparer for EventLogEntry.

public class EventLogEntryListComparison :
    IEqualityComparer<List<EventLogEntry>>,
    IEqualityComparer<EventLogEntry>

For IEqualityComparer<List<EventLogEntry>>, the GetHashCode function is very simple.

public int GetHashCode(List<EventLogEntry> obj)
{
    return obj.Sum(entry => 23 * GetHashCode(entry));
}

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.EventLogAnalysis\EventLogEntryListComparison.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.EventLogAnalysis\Program.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()

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;

How is it that LINQ's Sum function behaving differently?

Edit 2

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's Enumerable.Sum(...) methods perform the additions inside a checked block. This means that they deliberately throw an exception if the sum overflows.

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总和OverflowException异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆