LINQ Interview Question #106

What happens when a LINQ expression cannot be translated by EF Core?

LINQ Performance, EF Core LINQ, Expressions & Advanced Queries Senior Advanced

Quick Interview Answer

For parts of a query that must execute on the database, EF Core generally throws an exception when it cannot translate the expression rather than silently evaluating arbitrary filtering on the client.

Detailed Explanation

For parts of a query that must execute on the database, EF Core generally throws an exception when it cannot translate the expression rather than silently evaluating arbitrary filtering on the client.

If client-side processing is intentional, developers can explicitly cross the boundary with AsEnumerable or materialize the required data first. That decision should be made carefully because it can increase data transfer and memory usage.

Code Example

var serverQuery = db.Products
    .Where(p => p.IsActive);

var result = serverQuery
    .AsEnumerable()
    .Where(p => CustomDotNetMethod(p))
    .ToList();