InvalidOperationException: LINQ表达式的GroupByShaperExpression:
fileMovementRepository.GetAll() .Where(fm => repository.GetAll().Select(f => f.Id).Contains(fm.FileId) && fm.TransferredById == userId) .Include(f => f.User).Include(f => f.File).ThenInclude(f => f.Category) .OrderByDescending(f => f.MovedOn) .GroupBy(f => f.FileId) .Select(f=>f.First()) .ToList();
InvalidOperationException: LINQ表达式的GroupByShaperExpression:
fileMovementRepository.GetAll()
.Where(fm => repository.GetAll().Select(f => f.Id).Contains(fm.FileId) && fm.TransferredById == userId)
.Include(f => f.User).Include(f => f.File).ThenInclude(f => f.Category)
.OrderByDescending(f => f.MovedOn)
.GroupBy(f => f.FileId)
.Select(f=>f.First())
.ToList();
在运行时显示以下错误
处理请求时发生未处理的异常。InvalidOperationException: LINQ表达式'GroupByShaperExpression: KeySelector: f.FileId,ElementSelector:EntityShaperExpression: EntityType: FileMovement ValueBufferExpression: ProjectionBindingExpression: EmptyProjectionMember IsNullable: False
无法翻译‘.First()’。以可翻译的形式重写查询,或者通过插入对“AsEnumerable”、“AsAsyncEnumerable”、“ToList”或“ToListAsync”的调用显式切换到客户端计算。有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038。Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
InvalidOperationException:未能转换LINQ表达式'GroupByShaperExpression: KeySelector: f.FileId,ElementSelector:EntityShaperExpression: EntityType: FileMovement ValueBufferExpression: ProjectionBindingExpression: EmptyProjectionMember IsNullable: False .First()‘。以可翻译的形式重写查询,或者通过插入对“AsEnumerable”、“AsAsyncEnumerable”、“ToList”或“ToListAsync”的调用显式切换到客户端计算。有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038。
在GroupBy
之前使用ToList()
,它将按预期工作。
fileMovementRepository.GetAll() .Where(fm => repository.GetAll().Select(f => f.Id).Contains(fm.FileId) && fm.TransferredById == userId) .Include(f => f.User).Include(f => f.File).ThenInclude(f => f.Category) .OrderByDescending(f => f.MovedOn) .ToList() .GroupBy(f => f.FileId) .Select(f=>f.First()) .ToList();
转 https://cloud.tencent.com/developer/ask/sof/1142225/answer/1594419