{"record":{"id":"215f3a2302602e8e","repo":"dotnet/efcore","slug":"defaultifempty-cannot-be-applied-after-a-client","errorCode":null,"errorMessage":"'DefaultIfEmpty' cannot be applied after a client-evaluated projection. Consider applying 'DefaultIfEmpty' before last 'Select' or use 'AsEnumerable' before 'DefaultIfEmpty' to apply it on client-side.","messagePattern":"'DefaultIfEmpty' cannot be applied after a client-evaluated projection\\. Consider applying 'DefaultIfEmpty' before last 'Select' or use 'AsEnumerable' before 'DefaultIfEmpty' to apply it on client-side\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.InMemory/Query/Internal/InMemoryQueryExpression.cs","lineNumber":463,"sourceCode":"        {\n            throw new InvalidOperationException(InMemoryStrings.SetOperationsNotAllowedAfterClientEvaluation);\n        }\n\n        ServerQueryExpression = Call(\n            setOperationMethodInfo.MakeGenericMethod(typeof(ValueBuffer)), ServerQueryExpression, source2.ServerQueryExpression);\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 virtual void ApplyDefaultIfEmpty()\n    {\n        if (_clientProjections.Count != 0)\n        {\n            throw new InvalidOperationException(InMemoryStrings.DefaultIfEmptyAppliedAfterProjection);\n        }\n\n        ServerQueryExpression = Call(\n            EnumerableMethods.DefaultIfEmptyWithArgument.MakeGenericMethod(typeof(ValueBuffer)),\n            ServerQueryExpression,\n            Constant(new ValueBuffer(Enumerable.Repeat((object?)null, _projectionMappingExpressions.Count).ToArray())));\n\n        ReplaceProjection(\n            _projectionMapping.ToDictionary(\n                kv => kv.Key,\n                kv => kv.Value switch\n                {\n                    EntityProjectionExpression p => MakeEntityProjectionNullable(p),\n\n                    var p when !p.Type.IsNullableType()\n                        => Coalesce(\n                            MakeReadValueNullable(p),\n                            p.Type.GetDefaultValueConstant()),","sourceCodeStart":445,"sourceCodeEnd":481,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.InMemory/Query/Internal/InMemoryQueryExpression.cs#L445-L481","documentation":"ApplyDefaultIfEmpty throws when _clientProjections.Count != 0 (InMemoryQueryExpression.cs:461-463). DefaultIfEmpty needs the server-side structure intact to synthesise the empty/default row; after a client-evaluated projection that structure is gone.","triggerScenarios":"Calling DefaultIfEmpty() after a Select that causes client evaluation, e.g. db.Blogs.Select(b => new Dto(b)).DefaultIfEmpty().","commonSituations":"Manually implementing left-join semantics with Select(...).DefaultIfEmpty(), or applying DefaultIfEmpty on a projected collection. Often appears when translating hand-written SQL left joins to LINQ.","solutions":["Move DefaultIfEmpty before the last Select so it operates on server-evaluated data.","Use AsEnumerable() before DefaultIfEmpty to apply it client-side.","Replace manual DefaultIfEmpty with a navigation or GroupJoin which EF can translate."],"exampleFix":"// before\nvar q = db.Blogs.Select(b => new Dto(b)).DefaultIfEmpty();\n// after\nvar q = db.Blogs.DefaultIfEmpty().Select(b => b == null ? null : new Dto(b));","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    var r = query.Select(proj).DefaultIfEmpty().ToList();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"DefaultIfEmpty\"))\n{\n    var r = query.AsEnumerable().Select(proj).DefaultIfEmpty().ToList();\n}","preventionTips":["Call DefaultIfEmpty before the last Select, or use AsEnumerable() before it.","Prefer navigations/GroupJoin for left-join semantics over manual DefaultIfEmpty."],"tags":["ef-core","in-memory-provider","linq-translation","defaultifempty","left-join"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}