{"record":{"id":"853cc2ded663d259","repo":"litedb-org/LiteDB","slug":"new-instance-are-not-supported-for-node-type-whe","errorCode":null,"errorMessage":"New instance are not supported for {node.Type} when convert to BsonExpression ({node.ToString()}).","messagePattern":"New instance are not supported for (.+?) when convert to BsonExpression \\((.+?)\\)\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs","lineNumber":366,"sourceCode":"\n        /// <summary>\n        /// Visit :: x => `new { x.Id, x.Name }`\n        /// </summary>\n        protected override Expression VisitNew(NewExpression node)\n        {\n            if (node.Members == null)\n            {\n                if (TryGetResolver(node.Type, out var type))\n                {\n                    var pattern = type.ResolveCtor(node.Constructor);\n\n                    if (pattern == null) throw new NotSupportedException($\"Constructor for {node.Type.Name} are not supported when convert to BsonExpression ({node.ToString()}).\");\n\n                    this.ResolvePattern(pattern, null, node.Arguments);\n                }\n                else\n                {\n                    throw new NotSupportedException($\"New instance are not supported for {node.Type} when convert to BsonExpression ({node.ToString()}).\");\n                }\n            }\n            else\n            {\n                _builder.Append(\"{ \");\n\n                for (var i = 0; i < node.Members.Count; i++)\n                {\n                    var member = node.Members[i];\n                    _builder.Append(i > 0 ? \", \" : \"\");\n                    _builder.AppendFormat(\"'{0}': \", member.Name);\n                    this.Visit(node.Arguments[i]);\n                }\n\n                _builder.Append(\" }\");\n            }\n\n            return node;","sourceCodeStart":348,"sourceCodeEnd":384,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs#L348-L384","documentation":"Thrown by VisitNew when a new SomeType(args) expression is encountered where SomeType has NO registered resolver and node.Members is null (meaning it is a plain constructor call, not a member-init anonymous-type projection). LiteDB cannot construct arbitrary types via parameterized constructors in BsonExpression translation.","triggerScenarios":"Using new with a user-defined type that has constructor parameters inside a LINQ query. For example x => new MyDto(x.Id, x.Name) in a projection or predicate, where MyDto has no ITypeResolver.","commonSituations":"Projecting query results into DTOs or value objects with parameterized constructors inside the LINQ expression itself. Using value objects or records with primary constructors in query predicates.","solutions":["Use member-initializer syntax (new MyDto { Id = x.Id, Name = x.Name }) instead of a parameterized constructor, provided the type has a parameterless ctor.","Hoist the construction out of the expression and fetch-then-project in memory.","Register a custom ITypeResolver with a ResolveCtor pattern if translation is required."],"exampleFix":"// before -- parameterized ctor in query\nvar dtos = col.Find(x => x.Value > new Threshold(10).Limit);\n\n// after -- hoist the constant\nvar limit = new Threshold(10).Limit;\nvar dtos = col.Find(x => x.Value > limit);","handlingStrategy":"validation","validationCode":"// Avoid new CustomType(args) in query expressions; use member-initializers or hoist\nvar limit = new Threshold(10).Limit; // evaluate outside\ncol.Find(x => x.Value > limit);","typeGuard":null,"tryCatchPattern":"try\n{\n    var results = col.Find(x => x.Value > new Threshold(10).Limit);\n}\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"New instance are not supported for\"))\n{\n    var limit = new Threshold(10).Limit;\n    results = col.Find(x => x.Value > limit);\n}","preventionTips":["Never use new with parameterized constructors of custom types inside LINQ queries.","Use member-initializer syntax for anonymous-type projections, or hoist constants out.","Fetch first, then project in memory for complex DTO construction."],"tags":["linq","constructor","new-instance"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}