dotnet/efcore · error · InvalidOperationException
The LINQ expression '{expression}' could not be translated.
Error message
The LINQ expression '{expression}' could not be translated. Additional information: {details} Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information. What it means
InMemoryQueryTranslationPreprocessor.Process detects a non-composed GroupBy (GroupByWithKeySelector or GroupByWithKeyElementSelector as a top-level MethodCall) and throws TranslationFailedWithDetails with the NonComposedGroupByNotSupported detail string (InMemoryQueryTranslationPreprocessor.cs:39-44). The InMemory provider only supports GroupBy that is immediately followed by a composing aggregate; it cannot yield IGrouping sequences.
Source
Thrown at src/EFCore.InMemory/Query/Internal/InMemoryQueryTranslationPreprocessor.cs:42
QueryCompilationContext queryCompilationContext)
: base(dependencies, queryCompilationContext)
{
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override Expression Process(Expression query)
{
var result = base.Process(query);
return result is MethodCallExpression { Method.IsGenericMethod: true } methodCallExpression
&& (methodCallExpression.Method.GetGenericMethodDefinition() == QueryableMethods.GroupByWithKeySelector
|| methodCallExpression.Method.GetGenericMethodDefinition() == QueryableMethods.GroupByWithKeyElementSelector)
? throw new InvalidOperationException(
CoreStrings.TranslationFailedWithDetails(methodCallExpression.Print(), InMemoryStrings.NonComposedGroupByNotSupported))
: result;
}
/// <inheritdoc />
protected override bool IsEfConstantSupported
=> true;
}
View on GitHub (pinned to dbf9771522)
Solutions
- Compose the GroupBy with an aggregate immediately (Count/Sum/Min/Max/Average) so it is 'composed'.
- Call AsEnumerable() before GroupBy to perform grouping client-side.
- Restructure the query to avoid non-composed grouping entirely.
Example fix
// before
var q = db.Orders
.GroupBy(o => o.CustomerId)
.Select(g => g.OrderByDescending(x => x.Date).First());
// after
var q = db.Orders.AsEnumerable()
.GroupBy(o => o.CustomerId)
.Select(g => g.OrderByDescending(x => x.Date).First()); Defensive patterns
Strategy: try-catch
Try / catch
try
{
var r = query.GroupBy(k => k.A).Select(g => g.First()).ToList();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("could not be translated"))
{
var r = query.AsEnumerable().GroupBy(k => k.A).Select(g => g.First()).ToList();
} Prevention
- Compose GroupBy with an immediate aggregate for the InMemory provider.
- Call AsEnumerable() before GroupBy when you need per-group element access.
- Remember the InMemory provider does not support non-composed GroupBy.
When it happens
Trigger: Calling GroupBy and then doing anything other than an immediate aggregate on the grouping, e.g. .GroupBy(x => x.K).Select(g => g) or .GroupBy(...).Where(g => ...).
Common situations: Queries that work on relational providers (which can translate grouping) fail on the InMemory provider in tests. Common when grouping then selecting per-group elements or doing per-group ordering.
Related errors
- The LINQ expression '{expression}' could not be translated.
- Unable to translate set operation after client projection ha
- 'DefaultIfEmpty' cannot be applied after a client-evaluated
- Using 'Distinct' operation on a projection containing a subq
- The '{methodName}' method is not supported because the query
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/ef08ec546c1b0172.
Report an issue: GitHub.