{"record":{"id":"2f8fde80a186fb37","repo":"litedb-org/LiteDB","slug":"extend-expression-must-return-an-anonymous-class-t","errorCode":null,"errorMessage":"Extend expression must return an anonymous class to be merge with entities. Eg: `col.UpdateMany(x => new { Name = x.Name.ToUpper() }, x => x.Age > 10)`","messagePattern":"Extend expression must return an anonymous class to be merge with entities\\. Eg: `col\\.UpdateMany\\(x => new (.+?), x => x\\.Age > 10\\)`","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Database/Collections/Update.cs","lineNumber":82,"sourceCode":"\n            return _engine.UpdateMany(_collection, transform, predicate);\n        }\n\n        /// <summary>\n        /// Update many document based on merge current document with extend expression. Use your class with initializers. \n        /// Eg: col.UpdateMany(x => new Customer { Name = x.Name.ToUpper(), Salary: 100 }, x => x.Name == \"John\")\n        /// </summary>\n        public int UpdateMany(Expression<Func<T, T>> extend, Expression<Func<T, bool>> predicate)\n        {\n            if (extend == null) throw new ArgumentNullException(nameof(extend));\n            if (predicate == null) throw new ArgumentNullException(nameof(predicate));\n\n            var ext = _mapper.GetExpression(extend);\n            var pred = _mapper.GetExpression(predicate);\n\n            if (ext.Type != BsonExpressionType.Document)\n            {\n                throw new ArgumentException(\"Extend expression must return an anonymous class to be merge with entities. Eg: `col.UpdateMany(x => new { Name = x.Name.ToUpper() }, x => x.Age > 10)`\");\n            }\n\n            return _engine.UpdateMany(_collection, ext, pred);\n        }\n    }\n}","sourceCodeStart":64,"sourceCodeEnd":88,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Database/Collections/Update.cs#L64-L88","documentation":"ArgumentException thrown by the LINQ overload of UpdateMany when the extend expression does not resolve to a Document BsonExpression. The method merges the current entity with an anonymous-class initializer (x => new { Name = x.Name.ToUpper() }); if the mapper converts the lambda to a non-document expression, the merge target is undefined and the call is rejected.","triggerScenarios":"Passing a lambda that returns a scalar or a non-anonymous type the mapper cannot treat as a document, e.g. UpdateMany(x => x.Name.ToUpper(), x => x.Age > 10); or returning a single value instead of a member-initializer new { ... }.","commonSituations":"Using a projection that returns a primitive instead of an anonymous object; returning the entity type itself in a way the mapper collapses to a scalar; confusing the merge extend lambda with a value setter.","solutions":["Return an anonymous object with the fields to merge: UpdateMany(x => new { Name = x.Name.ToUpper() }, x => x.Age > 10).","Ensure every member you want to change is a property assignment inside the new { } initializer.","If updating the whole entity, use Update instead of UpdateMany."],"exampleFix":"// before\ncollection.UpdateMany(x => x.Name.ToUpper(), x => x.Age > 10);\n\n// after\ncollection.UpdateMany(x => new { Name = x.Name.ToUpper() }, x => x.Age > 10);","handlingStrategy":"validation","validationCode":"// Ensure the extend lambda returns an anonymous initializer\n// Compile-time guidance: the lambda body should be a NewExpression\nExpression<Func<T, T>> ext = x => new { Name = x.Name.ToUpper() }; // anonymous object\n// Validate at runtime via the mapper before calling UpdateMany:\nvar bsonExpr = mapper.GetExpression(ext);\nif (bsonExpr.Type != BsonExpressionType.Document)\n{\n    throw new InvalidOperationException(\"Extend lambda must project an anonymous object.\");\n}","typeGuard":"static bool IsAnonymousInit<TSource>(Expression<Func<TSource, TSource>> extend)\n    => extend.Body is NewExpression;","tryCatchPattern":"try\n{\n    collection.UpdateMany(extend, predicate);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"anonymous class\"))\n{\n    logger.LogError(\"UpdateMany extend lambda must return new { ... }.\");\n    throw;\n}","preventionTips":["Use new { Field = value } initializers in extend lambdas, not scalar returns.","Only assign the fields you want changed; the rest of the entity is preserved.","Use Update for whole-entity replacement instead of UpdateMany."],"tags":["update","linq","updatemany","expression","argument"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}