我需要将此SQL语句翻译成Linq-Entity查询...
SELECT name, count(name) FROM people
GROUP by name
Run Code Online (Sandbox Code Playgroud) 基本上我想做与这里相同的事情: https: //stackoverflow.com/a/16274992/4138686,但是在 EF Core 3.1.3 中使用这个答案给了我
System.InvalidOperationException: LINQ 表达式 '(GroupByShaperExpression: KeySelector: (r.Price), ElementSelector:(EntityShaperExpression: EntityType: RetailerRate ValueBufferExpression: (ProjectionBindingExpression: EmptyProjectionMember) IsNullable: False ) ) .OrderByDescending(r => r.CreatedAt)'无法翻译。以可翻译的形式重写查询,或者通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用来显式切换到客户端计算。有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038 。
这是代码:
await _db.RetailerRates
.GroupBy(r => r.Price)
.Select(g => g.OrderByDescending(r => r.CreatedAt).First())
.ToListAsync();
Run Code Online (Sandbox Code Playgroud)
有没有办法根据 EF Core 3.1 中每个组的日期检索第一个元素?