【发布时间】:2021-12-20 02:33:10
【问题描述】:
访问具有值的表: Id ImageId Score 。 需要得到每个模拟ImageId的最高分。
例如:
1 a1 10 points
2 a1 30 points
3 b1 15 points
想要:a1 30 points and b1 15 points
使用的代码:
public async Task<List<HistoryEntry>> Handle(Request request, CancellationToken cancellationToken)
=> await _db.History.GroupBy(a => a.ImageId).Select(s => s.OrderByDescending(x => x.Score).First()).ToListAsync();
}
错误:
.OrderByDescending(x => x.Score)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
InvalidOperationException: The LINQ expression 'GroupByShaperExpression: KeySelector: h.ImageId, ElementSelector:EntityShaperExpression: EntityType: HistoryEntry ValueBufferExpression: ProjectionBindingExpression: EmptyProjectionMember IsNullable: False .OrderByDescending(x => x.Score)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
【问题讨论】:
-
希望此功能将随 EF Core 6.0 一起提供,应该很快就会发布。在此之前,请使用链接帖子中的解决方法,在您的情况下类似于
_db.History.Select(a => a.ImageId).Distinct().SelectMany(key => _db.History.Where(x => x.ImageKey == key).OrderByDescending(x => x.Score).Take(1))
标签: c# linq entity-framework-core