为什么在没有额外的选择匿名步骤的情况下,GroupBy之后是SelectMany才能获取该组的n条最新记录的情况?

时间:2019-02-26 17:38:17

标签: entity-framework-core ef-core-2.1

问题标题在这里很难写,但我的意思是:

这正在工作。

DataContext.Article.AsNoTracking()
.GroupBy(a => a.ArticleCategoryId)
.Select(group =>
    new {
        Articles = group.Take(3)
    })
.Select(x => x.Articles)

这不起作用。

DataContext.Article.AsNoTracking()
.GroupBy(a => a.ArticleCategoryId)
.SelectMany(group => group.Take(3))

我收到此错误:

ArgumentException: Value does not fall within the expected range.
System.SharedTypeExtensions.GetSequenceType(Type type)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)
Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.LiftSubQuery(IQuerySource querySource, SubQueryExpression subQueryExpression)
Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.CompileAdditionalFromClauseExpression(AdditionalFromClause additionalFromClause, QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitAdditionalFromClause(AdditionalFromClause fromClause, QueryModel queryModel, int index)
Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitAdditionalFromClause(AdditionalFromClause fromClause, QueryModel queryModel, int index)
Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection<IBodyClause> bodyClauses, QueryModel queryModel)
Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.LiftSubQuery(IQuerySource querySource, SubQueryExpression subQueryExpression)
Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.CompileJoinClauseInnerSequenceExpression(JoinClause joinClause, QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitJoinClause(JoinClause joinClause, QueryModel queryModel, int index)
Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitJoinClause(JoinClause joinClause, QueryModel queryModel, int index)
Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection<IBodyClause> bodyClauses, QueryModel queryModel)
Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitSubQuery(SubQueryExpression expression)
System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ExpressionVisitorBase.VisitLambda<T>(Expression<T> node)

我真的不知道该怎么做。我在这里选择的匿名类型似乎是一个任意步骤,但可以使它起作用。

0 个答案:

没有答案