{"record":{"id":"ef08ec546c1b0172","repo":"dotnet/efcore","slug":"the-linq-expression-expression-could-not-be-tr-ef08ec","errorCode":null,"errorMessage":"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.","messagePattern":"The LINQ expression '(.+?)' could not be translated\\. Additional information: (.+?) 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\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.InMemory/Query/Internal/InMemoryQueryTranslationPreprocessor.cs","lineNumber":42,"sourceCode":"        QueryCompilationContext queryCompilationContext)\n        : base(dependencies, queryCompilationContext)\n    {\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public override Expression Process(Expression query)\n    {\n        var result = base.Process(query);\n\n        return result is MethodCallExpression { Method.IsGenericMethod: true } methodCallExpression\n            && (methodCallExpression.Method.GetGenericMethodDefinition() == QueryableMethods.GroupByWithKeySelector\n                || methodCallExpression.Method.GetGenericMethodDefinition() == QueryableMethods.GroupByWithKeyElementSelector)\n                ? throw new InvalidOperationException(\n                    CoreStrings.TranslationFailedWithDetails(methodCallExpression.Print(), InMemoryStrings.NonComposedGroupByNotSupported))\n                : result;\n    }\n\n    /// <inheritdoc />\n    protected override bool IsEfConstantSupported\n        => true;\n}\n","sourceCodeStart":24,"sourceCodeEnd":51,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.InMemory/Query/Internal/InMemoryQueryTranslationPreprocessor.cs#L24-L51","documentation":"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.","triggerScenarios":"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 => ...).","commonSituations":"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.","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."],"exampleFix":"// before\nvar q = db.Orders\n    .GroupBy(o => o.CustomerId)\n    .Select(g => g.OrderByDescending(x => x.Date).First());\n// after\nvar q = db.Orders.AsEnumerable()\n    .GroupBy(o => o.CustomerId)\n    .Select(g => g.OrderByDescending(x => x.Date).First());","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    var r = query.GroupBy(k => k.A).Select(g => g.First()).ToList();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"could not be translated\"))\n{\n    var r = query.AsEnumerable().GroupBy(k => k.A).Select(g => g.First()).ToList();\n}","preventionTips":["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."],"tags":["ef-core","in-memory-provider","linq-translation","groupby","client-evaluation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}