2

I've already tried some solutions that I found in other posts, but I'm still without success.

I have a list that I would like to add a value in a certain condition:

long teste = (long)joinHhResWithMemRes.Where(x => x.OcupacaoCode == 1).Sum(x => x.Weight);

but it always returns this error:

System.OverflowException: Arithmetic operation resulted in an overflow. at System.Linq.Enumerable.Sum(IEnumerable1 source) at EvoStats.Application.Services.AudienceDash.WeightGridCalcService.CalcWeightByTargert(IEnumerable1 joinHhResWithMemRes) in C:\IPCA\GfK\EvoTam\EvoTamStats\Server\EvoStats\src\EvoStats.Application\Services\AudienceDash\WeightGridCalcService.cs:line 350 at EvoStats.Application.Services.AudienceDash.WeightGridCalcService.GetWeightGridFromXmlAsync(DateTime date) in C:\IPCA\GfK\EvoTam\EvoTamStats\Server\EvoStats\src\EvoStats.Application\Services\AudienceDash\WeightGridCalcService.cs:line 51 at EvoStats.API.Controllers.XmlValidationController.GetWeightGrid(DateTime date) in C:\IPCA\GfK\EvoTam\EvoTamStats\Server\EvoStats\src\EvoStats.API\Controllers\XmlValidationController.cs:line 20 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

I've tested several types of variables, from long, ulong, double, decimal, but without success. I exported the list to excel and did the manual calculation and the final value is: "46219134368", so long should be enough.

FYI the list has 2322 rows.

I appreciate your help.

racl
  • 35
  • 1
  • 4

1 Answers1

2

You are still adding whatever type is x.Weight. Move the cast to inside the Sum:

long teste = joinHhResWithMemRes.Where(x => x.OcupacaoCode == 1).Sum(x => (long)x.Weight);
Ilian
  • 5,113
  • 1
  • 32
  • 41