System.invalidoperationexception:集合被修改;枚举操作可能无法在 .net 5 api 项目中执行

Posted

技术标签:

【中文标题】System.invalidoperationexception:集合被修改;枚举操作可能无法在 .net 5 api 项目中执行【英文标题】:System.invalidoperationexception: collection was modified; enumeration operation may not execute in .net 5 api project 【发布时间】:2022-01-09 23:01:37 【问题描述】:

代码在本地和开发环境中都可以正常工作,但是当我在舞台环境中发布代码时,我遇到了错误。我无法理解它仅在暂存环境中是如何发生的。

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.JoinIterator[TOuter,TInner,TKey,TResult](IEnumerable`1 outer, IEnumerable`1 inner, Func`2 outerKeySelector, Func`2 innerKeySelector, Func`3 resultSelector, IEqualityComparer`1 comparer)+MoveNext()
   at System.Linq.Enumerable.JoinIterator[TOuter,TInner,TKey,TResult](IEnumerable`1 outer, IEnumerable`1 inner, Func`2 outerKeySelector, Func`2 innerKeySelector, Func`3 resultSelector, IEqualityComparer`1 comparer)+MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.EnumerablePartition`1.ToList()
   at Soulmaker.Services.VideoService.GetClientDetailAsync(Int64 id, Int64 userId)
   at Soulmaker.API.Controllers.VideoController.GetClientDetails(Int32 id) in /src/Soulmaker.API/Controllers/VideoController.cs:line 184

这是我正在使用的代码,并在部署代码发出 API 请求后收到上述错误作为响应。

public async Task<VideoDetailsResponse> GetClientDetailAsync(long id, long userId)
       
           var video = await _context.Videos
                                 .Include(x => x.CreatedByUser)
                                 .Include(x => x.Like)
                                 .Include(x => x.PublishedByUser)
                                 .Include(x => x.ModifiedByUser)
                                 .Include(x => x.Tags)
                                 .ThenInclude(x => x.Topic)
                                 .Include(x => x.AdminTags)
                                 .ThenInclude(x => x.AdminTag)
                                 .FirstOrDefaultAsync(x => !x.IsDeleted && !x.IsHidden && x.Id == id);

           var response = _mapper.Map<VideoDetailsResponse>(video);
           response.IsLiked = video.Like.Any(x => x.UserId == userId);

           var items = (from topicId in video.Tags.Select(x => x.TopicId)
                        join tag in _context.VideoTagLink on topicId equals tag.TopicId
                        join videoItem in _context.Videos
                                           .Include(x => x.CreatedByUser)
                                           .Include(x => x.Like)
                                           .Include(x => x.PublishedByUser)
                                           .Include(x => x.ModifiedByUser)
                                           .Include(x => x.Tags)
                                           .ThenInclude(x => x.Topic)
                                           .Include(x => x.AdminTags)
                                           .ThenInclude(x => x.AdminTag)
                                           .Where(x => !x.IsDeleted && !x.IsHidden)
                         on tag.VideoId equals videoItem.Id
                         where tag.VideoId != id && !videoItem.IsDeleted
                         select videoItem).Take(2).ToList();

           var relatedExperiences = new List<RelatedExpirienceResponse>();
           for ( int i = 0; i < items.Count; i++ )
           
               var item = _mapper.Map<RelatedExpirienceResponse>(items[i]);
               item.IsLiked = items[i].Like.Any(x => x.UserId == userId);
               relatedExperiences.Add(item);
           
           response.RealtedExpiriences.AddRange(relatedExperiences);

           return response;
       

【问题讨论】:

【参考方案1】:

(来自CodeProject的交叉发布)

我怀疑这是因为您试图将内存中的列表加入数据库表。

试试:

var topicIds = video.Tags.Select(x => x.TopicId).ToList();
var items = (from tag in _context.VideoTagLink.Where(t => topicIds.Contains(t.TopicId))
             join videoItem in _context.Videos
                 .Include(x => x.CreatedByUser)
                 .Include(x => x.Like)
                 .Include(x => x.PublishedByUser)
                 .Include(x => x.ModifiedByUser)
                 .Include(x => x.Tags)
                 .ThenInclude(x => x.Topic)
                 .Include(x => x.AdminTags)
                 .ThenInclude(x => x.AdminTag)
                 .Where(x => !x.IsDeleted && !x.IsHidden)
             on tag.VideoId equals videoItem.Id
             where tag.VideoId != id && !videoItem.IsDeleted
             select videoItem).Take(2).ToList();

【讨论】:

以上是关于System.invalidoperationexception:集合被修改;枚举操作可能无法在 .net 5 api 项目中执行的主要内容,如果未能解决你的问题,请参考以下文章

视图中出现 InvalidOperationException.Collection 被修改错误