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:

浏览 23关注 0回答 1得票数 0
原文
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

 

 

Basharat Hussain
回答于2021-07-07 18:25已采纳
得票数 0

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

posted @ 2022-07-21 09:48  dreamw  阅读(108)  评论(0)    收藏  举报