question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query: Prevent client evaluation when using certain result operators in complex query

See original GitHub issue

Steps to reproduce

await
                        db.ProxyServerLoadResult.AsNoTracking()
                            .Include(x => x.ProxyServer)
                            .Where(x => !x.ProxyServer.BlockedByOperator)
                            .Where(x => x.SourceId == 1)
                            .OrderByDescending(x => x.Score)
                            .Take(100)
                            .Select(x => x.ProxyServer)
                            .ToListAsync();

The issue

I guess this warning is wrong. Am I right?

Exception message:

Warning as error exception for warning 'RelationalEventId.QueryClientEvaluationWarning': The LINQ expression 'join ProxyServer x.ProxyServer in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[RP.DataModel.ProxyServer]) on Property([x], "ProxyServerId") equals Property([x.ProxyServer], "ProxyServerId")' could not be translated and will be evaluated locally. To suppress this Exception use the DbContextOptionsBuilder.ConfigureWarnings API. ConfigureWarnings can be used when overriding the DbContext.OnConfiguring method or using AddDbContext on the application service provider.

Further technical details

EF Core version: 1.1.0 Operating system: Windows 10 Visual Studio version: VS2015U3

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
maumarcommented, Nov 23, 2016

@AsValeO as a workaround try putting Take(100) at the end of the query like so:

await
                        db.ProxyServerLoadResult.AsNoTracking()
                            .Include(x => x.ProxyServer)
                            .Where(x => !x.ProxyServer.BlockedByOperator)
                            .Where(x => x.SourceId == 1)
                            .OrderByDescending(x => x.Score)
                            .Select(x => x.ProxyServer)
                            .Take(100)
                            .ToListAsync();

this produces a much simpler query from the EF perspective, that we are able to fully translate at the moment. If possible, you should always try to put Skip/Take/Distinct as the last operation when using EF Core (at least until we address the current limitations).

The reason is that those are what we call ResultOperators - they are always the last operation in a given query. If there are more operations following, we introduce subqueries, which complicates the translation significantly for some cases. E.g. the original intermediate model representation for your original query is:

from ProxyServerLoadResult x in 
    (from ProxyServerLoadResult x in DbSet<ProxyServerLoadResult>
    join ProxyServer x.ProxyServer in DbSet<ProxyServer>
    on Property(x, "ProxyServerId") equals Property(x.ProxyServer, "ProxyServerId")
    where !(x.ProxyServer.BlockedByOperator)
    where x.SourceId == 1
    order by x.Score desc
    select x)
    .Take(__p_0)
join ProxyServer x.ProxyServer in DbSet<ProxyServer>
on Property(x, "ProxyServerId") equals Property(x.ProxyServer, "ProxyServerId")
select x.ProxyServer

and the proposed workaround:

(from ProxyServerLoadResult x in DbSet<ProxyServerLoadResult>
join ProxyServer x.ProxyServer in DbSet<ProxyServer>
on Property(x, "ProxyServerId") equals Property(x.ProxyServer, "ProxyServerId")
where !(x.ProxyServer.BlockedByOperator)
where x.SourceId == 1
order by x.Score desc
select x.ProxyServer)
.Take(__p_0)
0reactions
mguinnesscommented, Jun 8, 2017

@maumar I think I’ve encountered this issue also. When you say fixed, do you mean this will be included in the Entity Framework Core 2.0 release?

EDIT: I see it was included in preview 1 https://github.com/aspnet/EntityFramework/releases/tag/rel%2F2.0.0-preview1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Complex Query Operators - EF Core
This page describes some of the complex operators and their supported variations.
Read more >
Query optimization techniques in SQL Server: tips and tricks
In this blog post we will show you step by step some tips and tricks for successful Query optimization techniques in SQL Server....
Read more >
Chapter 4. Query Performance Optimization
In the previous chapter, we explained how to optimize a schema, which is one of the necessary conditions for high performance. But working...
Read more >
SQL Query Optimization: 12 Useful Performance Tuning ...
In the article, we are going to examine how to optimize SQL queries and improve query performance by using SQL query optimization tips...
Read more >
Optimize query computation | BigQuery
Best practices to optimize query performance. ... Query specific columns. ... use SELECT * EXCEPT to exclude one or more columns from the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found