Max and Average evaluated locally
See original GitHub issueHere is my query
var stats = (from team in context.Teams
let teamCount = context.Teams.Count()
let roundTeamCount = context.RoundTeams.Count(rt => rt.RoundId == roundId)
let averagePoint = context.Teams.Select(p => p.Point).Average()
let maxPoint = context.Teams.Max(t => t.Point)
select new { averagePoint, maxPoint, teamCount, roundTeamCount }
) .FirstOrDefault();
public class Team
{
public int Id{ get; set; }
public string Name { get; set; }
public int Point { get; set; }
}
public class RoundTeamPlayer
{
public int Id{ get; set; }
public int Point { get; set; }
public long RoundId { get; set; }
public Round Round { get; set; }
public long TeamId { get; set; }
public Team Team { get; set; }
}
After running query VS hangs for several seconds and get these messages:
Microsoft Visual Studio
---------------------------
Evaluating the function '<>f__AnonymousType35<<averagePoint>j__TPar, <maxPoint>j__TPar, <teamCount>j__TPar, <roundTeamCount>j__TPar>.averagePoint.get' timed out and needed to be aborted in an unsafe way. This may have corrupted the target process.
If the problem happens regularly, consider disabling the Tools->Options setting "Debugging->General->Enable property evaluation and other implicit function calls" or change the code to disable evaluation of this method. See help for information on doing this.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Average()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20502]
Possible unintended use of a potentially throwing aggregate method (Min, Max, Average) in a subquery. Client evaluation will be used and operator will throw if no data exists. Changing the subquery result type to a nullable type will allow full translation.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Max()' could not be translated and will be evaluated locally.
Translated sql queries:
Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (51ms) [Parameters=[@__8__locals1_roundId_0='?'], CommandType='Text', CommandTimeout='30']
SELECT TOP(1) (
SELECT COUNT(*)
FROM [Teams] AS [t2]
) AS [teamCount], (
SELECT COUNT(*)
FROM [RoundTeams] AS [rt]
WHERE [rt].[RoundId] = @__8__locals1_roundId_0
) AS [roundTeamCount]
FROM [Teams] AS [team]
Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (39ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT AVG(CAST([p1].[Point] AS float))
FROM [Teams] AS [p1]
Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (32ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT MAX([t1].[Point])
FROM [Teams] AS [t1]
When I use FirstOrDefaultAsync()
there no VS hangs but still get The LINQ expression 'Average()' ...
warns
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Max and Average evaluated locally · Issue #11797
Query[20500] The LINQ expression 'Max()' could not be translated and will be evaluated locally. Translated sql queries: Microsoft.
Read more >Local Maximum and Minimum
Local maximum and minimum are the points of the functions, which give the maximum and minimum range. The local maxima and local minima...
Read more >Entity Framework Core 2.2 Compiled Query Struct ...
RelationalQueryModelVisitor The LINQ expression 'Skip(__paging.Skip)' could not be translated and will be evaluated locally. warn: Microsoft.
Read more >Maximum & Minimum of a Function | Solution & Examples
Algebraically, to find local maximum or minimum, first, the first derivative of the function needs to be found. Values of x which makes...
Read more >Query Execution - ADO.NET
Some examples of singleton queries are Average, Count, First, and Max. These execute immediately because the query must produce a sequence ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@mo-esmp - While having a check, removes need for throwing behavior, it is pretty difficult pattern to recognize in code (rather than human-read), so EF Core does not account for it and still goes for throwing behavior causing client eval.
Putting a cast outside of the aggregate operator has no effect. You are still computing result over non-nullable type and saving it into nullable property. You need to introduce cast inside the operator. e.g.
let maxPoint = context.Teams.Max(t => (int?)t.Point)
@smitpatel I changed the query and I checked for null scenario but still same thing is happening. so the only way is changing
Point
property to nullable int ?