Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Error : Collection was modified; enumeration operation may not execute #8414


User avatar
0
troyfernando created
var estimateSubPortions = from o in pagedAndFilteredEstimateSubPortions
                                          select new GetEstimateSubPortionForViewDto()
                                          {
                                              EstimateSubPortion = new EstimateSubPortionDto
                                              {
                                                  EstimateHdId = o.EstimateHdId,
                                                  PortionId = o.PortionId,
                                                  ItemNo = o.ItemNo,
                                                  SubPortDesc = o.SubPortDesc,
                                                  Area = o.Area,
                                                  TotalMaterials = o.TotalMaterials,
                                                  TotalLabor = o.TotalLabor,
                                                  TotalOthMat = o.TotalOthMat,
                                                  Id = o.Id
                                              }
                                          };

 var totalCount = await filteredEstimateSubPortions.CountAsync();

**** error here below first time it loads afterwards it's ok *****


return new PagedResultDto<GetEstimateSubPortionForViewDto>(
                    totalCount,
                    await estimateSubPortions.ToListAsync()
                );

26 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Please show the complete method.

  • User Avatar
    0
    troyfernando created
    public async Task<PagedResultDto<GetEstimateSubPortionForViewDto>> GetAll(GetAllEstimateSubPortionsInput input)
             {
                try
                {
                    var filteredEstimateSubPortions = _estimateSubPortionRepository.GetAll()
                                .WhereIf(input.MinEstimateHdIdFilter != null, e => e.EstimateHdId == input.MinEstimateHdIdFilter)
                                .WhereIf(input.MinPortionIdFilter != null, e => e.PortionId == input.MinPortionIdFilter)
                                .WhereIf(input.MinItemNoFilter != null, e => e.ItemNo == input.MinItemNoFilter)
                                 ;
    
                    var pagedAndFilteredEstimateSubPortions = filteredEstimateSubPortions
                        .OrderBy(input.Sorting ?? "id asc")
                        .PageBy(input);
    
                    var estimateSubPortions = from o in pagedAndFilteredEstimateSubPortions
                                              select new GetEstimateSubPortionForViewDto()
                                              {
                                                  EstimateSubPortion = new EstimateSubPortionDto
                                                  {
                                                      EstimateHdId = o.EstimateHdId,
                                                      PortionId = o.PortionId,
                                                      ItemNo = o.ItemNo,
                                                      SubPortDesc = o.SubPortDesc,
                                                      Area = o.Area,
                                                      TotalMaterials = o.TotalMaterials,
                                                      TotalLabor = o.TotalLabor,
                                                      TotalOthMat = o.TotalOthMat,
                                                      Id = o.Id
                                                  }
                                              };
    
                    var totalCount = await filteredEstimateSubPortions.CountAsync();
    
                    return new PagedResultDto<GetEstimateSubPortionForViewDto>(
                        totalCount,
                        await estimateSubPortions.ToListAsync()
                    );
                }catch(Exception err)
                {
                    throw new UserFriendlyException(err.Message);
                }
             }
    
  • User Avatar
    0
    aaron created
    Support Team

    Try to evaluate totalCount before pagedAndFilteredEstimateSubPortions.

  • User Avatar
    0
    troyfernando created

    totalCount returns a value and still has error

  • User Avatar
    0
    aaron created
    Support Team

    Try to await ToListAsync() for pagedAndFilteredEstimateSubPortions:

    var pagedAndFilteredEstimateSubPortions = await filteredEstimateSubPortions
        .OrderBy(input.Sorting ?? "id asc")
        .PageBy(input)
        .ToListAsync();
    
  • User Avatar
    0
    troyfernando created

    Error : Index was outside the bounds of the array.

    Error : Destination array was not long enough. Check the destination index, length, and the array's lower bounds. Parameter name: destinationArray

  • User Avatar
    0
    aaron created
    Support Team

    Please show the stack trace and your updated code.

  • User Avatar
    0
    troyfernando created
    at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
    at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
    at Microsoft.EntityFrameworkCore.Query.Expressions.SelectExpression.set_IsProjectStar(Boolean value)
    at Microsoft.EntityFrameworkCore.Query.Expressions.SelectExpression.PushDownSubquery()
    at Microsoft.EntityFrameworkCore.SqlServer.Query.Sql.Internal.SqlServerQuerySqlGenerator.RowNumberPagingExpressionVisitor.VisitSelectExpression(SelectExpression selectExpression)
    at Microsoft.EntityFrameworkCore.SqlServer.Query.Sql.Internal.SqlServerQuerySqlGenerator.RowNumberPagingExpressionVisitor.Visit(Expression expression)
    at Microsoft.EntityFrameworkCore.SqlServer.Query.Sql.Internal.SqlServerQuerySqlGeneratorFactory.CreateDefault(SelectExpression selectExpression)
    at Microsoft.EntityFrameworkCore.Query.Expressions.SelectExpression.CreateDefaultQuerySqlGenerator()
    at Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext.GetRelationalCommand(IReadOnlyDictionary`2 parameters) at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.BufferlessMoveNext(DbContext _, Boolean buffer, CancellationToken cancellationToken)_
    _at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)_
    _at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.MoveNext(CancellationToken cancellationToken) at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator`2.MoveNextCore(CancellationToken cancellationToken) in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\Select.cs:line 106_
    _at System.Linq.AsyncEnumerable.AsyncIterator`1.MoveNext(CancellationToken cancellationToken) in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\AsyncIterator.cs:line 98 at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)_
    _at System.Linq.AsyncEnumerable.Aggregate_[TSource,TAccumulate,TResult](IAsyncEnumerable`1 source, TAccumulate seed, Func`3 accumulator, Func`2 resultSelector, CancellationToken cancellationToken) in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\Aggregate.cs:line 120
    at DNEthan.Estimates.EstimateSubPortionsAppService.GetAll(GetAllEstimateSubPortionsInput input) in C:\Users\RJ\Downloads\DNEthanZero\DNEthan6\src\DNEthan.Application\Estimates\EstimateSubPortionsAppService.cs:line 79
    
  • User Avatar
    0
    troyfernando created
    public async Task<PagedResultDto<GetEstimateSubPortionForViewDto>> GetAll(GetAllEstimateSubPortionsInput input)
             {
                try
                {
                    var filteredEstimateSubPortions = _estimateSubPortionRepository.GetAll()
                                .WhereIf(input.MinEstimateHdIdFilter != null, e => e.EstimateHdId == input.MinEstimateHdIdFilter)
                                .WhereIf(input.MinPortionIdFilter != null, e => e.PortionId == input.MinPortionIdFilter)
                                .WhereIf(input.MinItemNoFilter != null, e => e.ItemNo == input.MinItemNoFilter)
                                 ;
    
                    var pagedAndFilteredEstimateSubPortions = await filteredEstimateSubPortions
                        .OrderBy(input.Sorting ?? "id asc")
                        .PageBy(input).ToListAsync();
    
                    var estimateSubPortions = from o in pagedAndFilteredEstimateSubPortions
                                              select new GetEstimateSubPortionForViewDto()
                                              {
                                                  EstimateSubPortion = new EstimateSubPortionDto
                                                  {
                                                      EstimateHdId = o.EstimateHdId,
                                                      PortionId = o.PortionId,
                                                      ItemNo = o.ItemNo,
                                                      SubPortDesc = o.SubPortDesc,
                                                      Area = o.Area,
                                                      TotalMaterials = o.TotalMaterials,
                                                      TotalLabor = o.TotalLabor,
                                                      TotalOthMat = o.TotalOthMat,
                                                      Id = o.Id
                                                  }
                                              };
    
                    var totalCount = await filteredEstimateSubPortions.CountAsync();
    
                    return new PagedResultDto<GetEstimateSubPortionForViewDto>(
                        totalCount,
                        estimateSubPortions.ToList()
                    );
                }catch(Exception err)
                {
                    throw new UserFriendlyException(err.Message);
                }
             }
    
  • User Avatar
    0
    aaron created
    Support Team

    Please format the stack trace and your code properly:

    ```csharp
    public async Task<PagedResultDto> GetAll(GetAllEstimateSubPortionsInput input)
    {
        ...
    }
    ```
    
  • User Avatar
    0
    aaron created
    Support Team
    • Which line of code is EstimateSubPortionsAppService.cs:line 79?
    • As I suggested before, please evaluate totalCount before pagedAndFilteredEstimateSubPortions.
  • User Avatar
    0
    troyfernando created

    ok

  • User Avatar
    0
    troyfernando created

    Which line of code is EstimateSubPortionsAppService.cs:line 79? previous solution

    var pagedAndFilteredEstimateSubPortions = await filteredEstimateSubPortions
        .OrderBy(input.Sorting ?? "id asc")
        .PageBy(input)
        .ToListAsync();
        ```
    it has error
    
  • User Avatar
    0
    aaron created
    Support Team

    That seems odd; there is similar code in several places in ASP<span></span>.NET Zero.

    Try ToList().

  • User Avatar
    0
    troyfernando created

    still with error

  • User Avatar
    0
    aaron created
    Support Team

    Does _estimateSubPortionRepository.GetAll().ToList() work?

  • User Avatar
    0
    troyfernando created

    same error with _estimateSubPortionRepository.GetAll().ToList()

  • User Avatar
    0
    troyfernando created

    any suggestions ? whole function was generated using powertools

  • User Avatar
    0
    aaron created
    Support Team

    It should not be the same error. Please check and show the stack trace.

  • User Avatar
    0
    troyfernando created

    does not recognize WhereIf

  • User Avatar
    0
    troyfernando created

    I noticed all your other GetAll(Input param) functions return the same error "Collection was modified; enumeration operation may not execute" on first load

  • User Avatar
    0
    aaron created
    Support Team

    Which version of ASP<span></span>.NET Zero are you on?

  • User Avatar
    0
    troyfernando created

    019-10-24 ASP.NET CORE & Angular .NET Core 2.2 v7.2.3

  • User Avatar
    0
    troyfernando created

    found the problem.

    angular is calling the GetAll(Input param) function twice

  • User Avatar
    0
    aaron created
    Support Team

    So this question can be closed?