• <tfoot id='jNsmJ'></tfoot>
    1. <legend id='jNsmJ'><style id='jNsmJ'><dir id='jNsmJ'><q id='jNsmJ'></q></dir></style></legend>

      <i id='jNsmJ'><tr id='jNsmJ'><dt id='jNsmJ'><q id='jNsmJ'><span id='jNsmJ'><b id='jNsmJ'><form id='jNsmJ'><ins id='jNsmJ'></ins><ul id='jNsmJ'></ul><sub id='jNsmJ'></sub></form><legend id='jNsmJ'></legend><bdo id='jNsmJ'><pre id='jNsmJ'><center id='jNsmJ'></center></pre></bdo></b><th id='jNsmJ'></th></span></q></dt></tr></i><div id='jNsmJ'><tfoot id='jNsmJ'></tfoot><dl id='jNsmJ'><fieldset id='jNsmJ'></fieldset></dl></div>
        <bdo id='jNsmJ'></bdo><ul id='jNsmJ'></ul>

        <small id='jNsmJ'></small><noframes id='jNsmJ'>

      1. LINQ 和溢出异常?

        时间:2023-06-05
          <tbody id='TmCmi'></tbody>
        <i id='TmCmi'><tr id='TmCmi'><dt id='TmCmi'><q id='TmCmi'><span id='TmCmi'><b id='TmCmi'><form id='TmCmi'><ins id='TmCmi'></ins><ul id='TmCmi'></ul><sub id='TmCmi'></sub></form><legend id='TmCmi'></legend><bdo id='TmCmi'><pre id='TmCmi'><center id='TmCmi'></center></pre></bdo></b><th id='TmCmi'></th></span></q></dt></tr></i><div id='TmCmi'><tfoot id='TmCmi'></tfoot><dl id='TmCmi'><fieldset id='TmCmi'></fieldset></dl></div>
        • <bdo id='TmCmi'></bdo><ul id='TmCmi'></ul>

              <tfoot id='TmCmi'></tfoot>

                • <small id='TmCmi'></small><noframes id='TmCmi'>

                • <legend id='TmCmi'><style id='TmCmi'><dir id='TmCmi'><q id='TmCmi'></q></dir></style></legend>

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

                  问题描述

                  限时送ChatGPT账号..

                  我已经为 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 和溢出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 SUM 和 ORDER BY 进行 Linq 查询 下一篇:Sum 多维数组 C#

                  相关文章

                  最新文章

                    <bdo id='1JeTf'></bdo><ul id='1JeTf'></ul>

                  1. <tfoot id='1JeTf'></tfoot>

                  2. <i id='1JeTf'><tr id='1JeTf'><dt id='1JeTf'><q id='1JeTf'><span id='1JeTf'><b id='1JeTf'><form id='1JeTf'><ins id='1JeTf'></ins><ul id='1JeTf'></ul><sub id='1JeTf'></sub></form><legend id='1JeTf'></legend><bdo id='1JeTf'><pre id='1JeTf'><center id='1JeTf'></center></pre></bdo></b><th id='1JeTf'></th></span></q></dt></tr></i><div id='1JeTf'><tfoot id='1JeTf'></tfoot><dl id='1JeTf'><fieldset id='1JeTf'></fieldset></dl></div>

                    1. <small id='1JeTf'></small><noframes id='1JeTf'>

                      <legend id='1JeTf'><style id='1JeTf'><dir id='1JeTf'><q id='1JeTf'></q></dir></style></legend>