{"record":{"id":"a2c39fdc4119dec6","repo":"dotnet/efcore","slug":"the-linq-expression-expression-could-not-be-tr-a2c39f","errorCode":null,"errorMessage":"The LINQ expression '{expression}' could not be translated. 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\\. 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/InMemoryProjectionBindingExpressionVisitor.cs","lineNumber":117,"sourceCode":"            {\n                switch (expression)\n                {\n                    case ConstantExpression:\n                        return expression;\n\n                    case ProjectionBindingExpression projectionBindingExpression:\n                        var mappedProjection = _queryExpression.GetProjection(projectionBindingExpression);\n                        if (mappedProjection is EntityProjectionExpression entityProjection)\n                        {\n                            return AddClientProjection(entityProjection, typeof(ValueBuffer));\n                        }\n\n                        if (mappedProjection is not InMemoryQueryExpression)\n                        {\n                            return AddClientProjection(mappedProjection, expression.Type.MakeNullable());\n                        }\n\n                        throw new InvalidOperationException(CoreStrings.TranslationFailed(projectionBindingExpression.Print()));\n\n                    case MaterializeCollectionNavigationExpression materializeCollectionNavigationExpression:\n                    {\n                        var subquery = _queryableMethodTranslatingExpressionVisitor.TranslateSubquery(\n                            materializeCollectionNavigationExpression.Subquery)!;\n                        _clientProjections!.Add(subquery.QueryExpression);\n                        return new CollectionResultShaperExpression(\n                            new ProjectionBindingExpression(\n                                _queryExpression, _clientProjections.Count - 1, typeof(IEnumerable<ValueBuffer>)),\n                            subquery.ShaperExpression,\n                            materializeCollectionNavigationExpression.Navigation,\n                            materializeCollectionNavigationExpression.Navigation.ClrType.GetSequenceType());\n                    }\n\n                    case MethodCallExpression methodCallExpression:\n                        if (methodCallExpression.Method.IsGenericMethod\n                            && methodCallExpression.Method.DeclaringType == typeof(Enumerable)\n                            && methodCallExpression.Method.Name == nameof(Enumerable.ToList)","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.InMemory/Query/Internal/InMemoryProjectionBindingExpressionVisitor.cs#L99-L135","documentation":"The InMemory query provider's projection binding visitor fell back to index-based (client-evaluation) binding and encountered a ProjectionBindingExpression whose server projection resolves to a nested InMemoryQueryExpression (a subquery). Subqueries cannot be lifted into the client projection list as a scalar, so translation aborts. This is EF's generic 'could not be translated' error surfaced because the InMemory provider has limited query-translation capability compared to relational providers.","triggerScenarios":"A LINQ query against an InMemory DbSet whose final projection contains a correlated subquery (e.g. an aggregate or scalar sub-select inside Select) that the InMemory translator cannot fold into the server query. The first server-side translation pass already failed, so the visitor retries with index-based binding and still cannot handle the nested query expression.","commonSituations":"Queries that run fine on SQL Server/SQLite but fail on the InMemory provider (e.g. in unit tests) because the projection contains subqueries, custom functions, or operators the InMemory translator does not understand. Common when projecting related aggregates like 'new { Blog = b, LatestPost = b.Posts.OrderByDescending(p => p.Id).FirstOrDefault() }'.","solutions":["Insert AsEnumerable()/ToList() before the Select that contains the subquery so the projection runs client-side.","Rewrite the projection to avoid nested subqueries; fetch aggregates in a separate query and join client-side.","Simplify the projection to only mapped scalar/entity properties the InMemory provider can translate."],"exampleFix":"// before\nvar q = db.Blogs\n    .Select(b => new { b, Count = db.Posts.Count(p => p.BlogId == b.Id) });\n// after\nvar q = db.Blogs.AsEnumerable()\n    .Select(b => new { b, Count = db.Posts.Count(p => p.BlogId == b.Id) });","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    foreach (var item in query) { /* ... */ }\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"could not be translated\"))\n{\n    // fall back to client evaluation\n    foreach (var item in query.AsEnumerable()) { /* ... */ }\n}","preventionTips":["Keep projections simple for the InMemory provider; avoid correlated subqueries inside Select.","Run integration tests against the InMemory provider to catch translation gaps early, but prefer the real provider for query-logic tests.","Use AsEnumerable()/ToList() before client-side computation in the projection."],"tags":["ef-core","in-memory-provider","linq-translation","query","client-evaluation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}