InvalidOperationException:LINQ 表达式 'GroupByShaperExpression:

Bas*_*ain 6 linq entity-framework-core asp.net-core

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();
Run Code Online (Sandbox Code Playgroud)

运行时显示以下错误

处理请求时发生未处理的异常。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.RelationalSqlTranslatedExpressionVisitor.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 。

Bas*_*ain 8

ToList()之前使用GroupBy它会按预期工作。

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();
Run Code Online (Sandbox Code Playgroud)