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.

EF.CompileQuery The LINQ expression could not be translated.

See original GitHub issue

Ask a question

EF.CompileQuery((DbContext context) => context.Set<T>().AsNoTracking().Where(filter).ToList())(this); Message=The LINQ expression ‘DbSet<LhUser>() .Where(l => l.Id == 1) .ToList()’ 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 ‘AsEnumerable’, ‘AsAsyncEnumerable’, ‘ToList’, or ‘ToListAsync’. I use the EF core 5.0.7,but the sdks is net core 3.1, Remember:

  • Please make your question as clear and specific as possible.
  • Please check that the documentation does not answer your question.
  • Please search in both open and closed issues to check that your question has not already been answered.

Include your code

Usually the best way to ask a clear question and get a quick response is to show your code. Preferably, attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing.

Use triple-tick code fences for any posted code. For example:

Console.WriteLine("Hello, World!");

Include stack traces

Include the full exception message and stack trace for any exception you encounter.

Use triple-tick fences for stack traces. For example:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at SixFour.Sub() in C:\Stuff\AllTogetherNow\SixFour\SixFour.cs:line 49
   at SixFour.Main() in C:\Stuff\AllTogetherNow\SixFour\SixFour.cs:line 54

Include verbose output

Please include verbose output when asking questions about the dotnet ef or Package Manager Console tools.

Use triple-tick fences for tool output. For example:

C:\Stuff\AllTogetherNow\FiveOh>dotnet ef dbcontext list --verbose
Using project 'C:\Stuff\AllTogetherNow\FiveOh\FiveOh.csproj'.
...
Finding DbContext classes in the project...
Found DbContext 'BlogContext'.
BlogContext

Include provider and version information

EF Core version: Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET 5.0) Operating system: IDE: (e.g. Visual Studio 2019 16.3)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
smitpatelcommented, Jul 30, 2021

Not opposing though

  • EF.CompileQuery needs a redesign. There is tracking issue for it.
  • The tracking issue is blocked because it needs to prove that it is useful perf wise too. We added it because EF6 had it and certainly we have customers out there using it. But does it make sense to write new queries using compiled query if perf difference is negligible. People porting over old EF6 queries, we should try to make that work.
  • Our fancy exception message (though I guess we should change it to point to the obvious) contains the query tree in which last method call/operator is the one which caused translation to fail. In this case it is ToList 😉
  • This would be different infra than currently what we have for translation failures.
    • ToList in normal queries work in the sense that we never see top level ToList - that is how IQueryable works
    • Any ToList we see are in subquery so we are materializing collection for it and we process it correctly
    • That means we need a special way to find out that it is CompiledQuery, a hook which we currently don’t have. We treat expression tree for any query same once it enters query compiler.
0reactions
ajcvickerscommented, Jul 30, 2021

Note from triage: added a note to #14551 to consider when re-visiting compiled query API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - The LINQ expression could not be translated and will ...
I got this error by querying my LINQ methods in the wrong order before calling a ToListAsync call at the end. Here was...
Read more >
The Query (LINQ) expression could not be Translated ...
Entity Framework Core evaluates a LINQ query on the server side as much as possible. · As shown in the above example, If...
Read more >
LINQ Expression Could not Be Translated - Microsoft Q&A
Hello everyone and thanks for the help in advance. I am creating a MVC application uisng .Net 6 and EF 6 to query...
Read more >
Invalidoperationexception: the LINQ expression
You are misusing the property and EF cannot translate that. In order to use an expression in LINQ it must be translatable to...
Read more >
Why a simplest ef core query throws 'cannot be translated ...
This seems very strange. The expression you are using for Select is placed into Where section of the genrated LINQ expression. Is it...
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