The code is working fine at local as well as dev environment but when I published the code at stage environment I am getting below error. I am not able to understand how it happens in a staging environment only.
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
Here is the code I am using and getting the above error as a response after making an API request at deployed code.
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;
}
(Cross-posted from CodeProject )
I suspect it's because you're trying to join an in-memory list to a database table.
Try:
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.