dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.82k stars 3.2k forks source link

Group by + orderby query no longer working #18077

Closed Looooooka closed 2 years ago

Looooooka commented 5 years ago

This query worked fine in EF Core 2 datacontext.data_Rewards.GroupBy(x => x.RewardGameType, (key, g) => g.OrderByDescending(e =>e.RewardPriority).FirstOrDefault()).ToList();`` in EF Core 3 it fails with

nvalidOperationException: The LINQ expression 'OrderByDescending<data_Rewards, int>( source: GroupByShaperExpression: KeySelector: d.RewardGameType, ElementSelector:EntityShaperExpression: EntityType: data_Rewards ValueBufferExpression: ProjectionBindingExpression: EmptyProjectionMember IsNullable: False , keySelector: (e) => e.RewardPriority)' 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(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

The original query produced an orderby SQL query and another group by query. Is there a way to at least turn back this default behavior or does one simply have to fix all the queries and make them work so they're correctly evaluated in sql server in a sinqle query?

I did rewrite this one but...I'm just afraid that a lot of queries might now just fail after updating

smitpatel commented 5 years ago

By design https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#linq-queries-are-no-longer-evaluated-on-the-client

See #17068 & #13805