相关疑难解决方法(0)

如何使用 EF 3.1 为实体框架 GroupBy 中的每个组选择前 N 行

我需要为带有实体框架的表中的每个组获取前 10 行。基于 SO 上的其他解决方案,我尝试了两件事:

var sendDocuments = await context.Set<DbDocument>
    .Where(t => partnerIds.Contains(t.SenderId))
    .GroupBy(t => t.SenderId)
    .Select(t => new
    {
        t.Key,
        Documents = t.OrderByDescending(t2 => t2.InsertedDateTime).Take(10)
    })                
    .ToArrayAsync();
Run Code Online (Sandbox Code Playgroud)

错误:

System.InvalidOperationException: 'The LINQ expression
'(GroupByShaperExpression: KeySelector: (d.SenderId), 
ElementSelector:(EntityShaperExpression: 
    EntityType: DbDocument
    ValueBufferExpression: 
        (ProjectionBindingExpression: EmptyProjectionMember)
    IsNullable: False ) )
    .OrderByDescending(t2 => t2.InsertedDateTime)' 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
> either AsEnumerable(), AsAsyncEnumerable(), …
Run Code Online (Sandbox Code Playgroud)

entity-framework group-by entity-framework-core ef-core-3.1

12
推荐指数
1
解决办法
4354
查看次数