Problem Scenario
If you write a simple LINQ query using Any() In EF Core, but at runtime, you get an exception like:
The LINQ expression could not be translated.
This usually happens only at runtime, making it frustrating to debug.

Let’s Discover Why This Happens
EF Core translates LINQ expressions into SQL.
The exception occurs when:
Any()is used with unsupported logic- Navigation properties are not properly included
- EF Core cannot convert part of the query to SQL
- Client-side evaluation is forced (which EF Core blocks)
Example That Fails
var result = context.Orders
.Where(o => o.Items.Any(i => CustomMethod(i.Price)))
.ToList();❌ EF Core cannot translate CustomMethod() into SQL.
✅ Correct Way to Fix It
✔ Solution 1: Move logic into SQL-translatable expressions
var result = context.Orders
.Where(o => o.Items.Any(i => i.Price > 100))
.ToList();✔ Solution 2: Use AsEnumerable() only when the data size is small
var result = context.Orders
.AsEnumerable()
.Where(o => o.Items.Any(i => CustomMethod(i.Price)))
.ToList();⚠️ Use this carefully; it pulls data into memory.
✔ Solution 3: Rewrite Using EXISTS
EF Core translates Any() into SQL EXISTS, but explicit filtering helps:
var result = context.Orders
.Where(o => o.Items.Select(i => i.OrderId).Contains(o.Id))
.ToList();Best Practices
- Avoid custom methods inside LINQ
- Log generated SQL using:
optionsBuilder.LogTo(Console.WriteLine);- Always test queries against production-like data