dotnet runtime_exception ai_generated true

System.InvalidOperationException: The LINQ expression could not be translated.

ID: dotnet/linq-translation-failure

Also available as: JSON · Markdown
88%Fix Rate
90%Confidence
80Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
8 active

Root Cause

EF Core throws this when a LINQ expression contains C# methods or logic that cannot be translated to SQL. Common culprits include custom methods, string interpolation in predicates, unsupported .NET method calls (e.g., TimeSpan arithmetic, regex), and complex conditional logic. Fix by restructuring the query to use translatable operations, or by explicitly evaluating the untranslatable part client-side with AsEnumerable()/ToList() at the right boundary.

generic

Workarounds

  1. 90% success Split the query into a translatable server-side part and a client-side post-filter
    Move the translatable filters (simple comparisons, Contains, etc.) before a materialization call, then apply the untranslatable logic after: var results = await dbContext.Orders.Where(o => o.Status == "Active").ToListAsync(); var filtered = results.Where(o => MyCustomMethod(o.Data)).ToList(); This keeps the expensive filtering on the server and only evaluates the custom logic on the reduced result set
  2. 88% success Replace the untranslatable method with an EF Core-compatible expression
    Identify which method cannot be translated and replace it with an EF-translatable equivalent. Common replacements: use EF.Functions.Like() instead of Regex, use .Contains() instead of custom string methods, use DbFunction mapping for database-specific functions, and inline simple logic instead of calling helper methods. Example: replace .Where(x => IsValid(x.Name)) with .Where(x => x.Name != null && x.Name.Length > 0)
  3. 85% success Use a compiled raw SQL query or FromSqlRaw for complex server-side logic
    When the required logic is expressible in SQL but not in LINQ, use raw SQL: var results = await dbContext.Orders.FromSqlRaw("SELECT * FROM Orders WHERE DATEDIFF(day, CreatedAt, GETDATE()) > {0}", days).ToListAsync(); Combine with .Where() and .OrderBy() for additional LINQ-translatable filters that EF Core will compose into the final query

Dead Ends

Common approaches that don't work:

  1. Switching the entire query to client-side evaluation by calling .ToList() before the Where clause 85% fail

    Loads the entire table into memory before filtering, causing catastrophic performance on large datasets. This masks the real issue and creates O(n) memory consumption and network transfer that will fail at scale

  2. Downgrading to EF Core 2.x where client evaluation was silently allowed 95% fail

    EF Core 2.x silently evaluated untranslatable expressions on the client, hiding severe performance bugs. EF Core 3.0+ throws this exception intentionally to prevent accidental full-table scans. Downgrading reintroduces those hidden performance problems and loses years of bug fixes and features

  3. Wrapping the untranslatable method in a try-catch and falling back to a different query 90% fail

    The exception is thrown during query compilation, not execution. The entire query fails before any SQL is sent to the database. A try-catch around the enumeration does not help because the translation failure is deterministic and will always throw for the same query shape

Error Chain

Leads to:
Preceded by:
Frequently confused with: