The Query (LINQ) expression could not be Translated Entity Framework Core

Issue Description

LINQ expression in Entity Framework core runtime gives below error or similar error.

The LINQ expression ‘DbSet
.Where(e => e.EmployeeId
.GetEmployeeName().Contains(“007”))’ 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(),

The Query LINQ expression could not be Translated Entity Framework Core

Resolution

The issue is more visible in the new .NET Core like .NET Core 3.0 and above..and could also be producible in other versions depending on the scenario.

This issue is also producible if you migrate Old .NET Core 2.0 + version-based code with LINQ queries to new .NET Core version 3.0 and above.

The issue can be fixed by understanding below best practices,

  • Entity Framework Core evaluates a LINQ query on the server side as much as possible.
  • Entity Framework Core blocks any client evaluation. Example: If EFCore detects any expression like the below example and blocks it,

The Query LINQ expression could not be Translated Entity Framework Core

  • As shown in the above example, If EF Core detects any similar expression (in any place other than the top-level projection) then it throws a runtime exception.
  • .NET Core 3.1 and above doesn’t support client evaluation.

Please refer to below article to understand more,

Resolution 1 – Service-Side Query evaluation

  • One can avoid any expression in the query as EFCore doesn’t understand and doesn’t translate it to the server.

In such cases, you can evaluate any expression prior to the query and send the value evaluated in the query. Below is a sample example that resolves my above issue.

The Query LINQ expression could not be Translated Entity Framework Core

The above use of expression outside of the query instructs the compiler to execute the whole query on the server-side.

Query (LINQ) expression -Approach2

I found this approach working fine too.

Calling query Enumerable() or using generics ToList(), ToArray() or ToListAsync() make the code execution immediately on the client side.

This approach, however, has performance implications and could cause memory leak issues if you are dealing with large data. You can use this option if dealing with small data.

The Query LINQ expression could not be Translated Entity Framework Core

The above use of AsEnumerable() instructs the compiler to execute the whole query on the client-side.

That’s all, Hope this helps you!

References:

Did I miss anything else in these resolution steps?

Did the above steps resolve your issue? Please sound off your comments below!

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



2 thoughts on “The Query (LINQ) expression could not be Translated Entity Framework Core

Leave a Reply

Your email address will not be published. Required fields are marked *