LINQ Interview Question #114

Why can calling methods inside an IQueryable predicate be problematic?

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

Quick Interview Answer

A custom .NET method may not have a known translation in the query provider. If the provider cannot convert the method call into SQL or another supported operation, query translation can fail.

Detailed Explanation

A custom .NET method may not have a known translation in the query provider. If the provider cannot convert the method call into SQL or another supported operation, query translation can fail.

Prefer expressions composed from provider-supported operations. If custom .NET processing is required, intentionally perform it after a suitable server-side query has reduced the data.

Code Example

// Usually provider-friendly:
var query = db.Products
    .Where(p => p.Name.StartsWith(prefix));

// A custom method may not be translatable:
// .Where(p => MyCustomCheck(p.Name))