{"record":{"id":"94faf42482b1ef2f","repo":"litedb-org/LiteDB","slug":"method-round-need-2-arguments-when-convert-to-bson","errorCode":null,"errorMessage":"Method Round need 2 arguments when convert to BsonExpression","messagePattern":"Method Round need 2 arguments when convert to BsonExpression","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Mapper/Linq/TypeResolver/MathResolver.cs","lineNumber":23,"sourceCode":"using System.Linq.Expressions;\nusing System.Reflection;\nusing System.Text;\nusing static LiteDB.Constants;\n\nnamespace LiteDB\n{\n    internal class MathResolver : ITypeResolver\n    {\n        public string ResolveMethod(MethodInfo method)\n        {\n            var qtParams = method.GetParameters().Length;\n\n            switch (method.Name)\n            {\n                case \"Abs\": return \"ABS(@0)\";\n                case \"Pow\": return \"POW(@0, @1)\";\n                case \"Round\":\n                    if (qtParams != 2) throw new ArgumentOutOfRangeException(\"Method Round need 2 arguments when convert to BsonExpression\");\n                    return \"ROUND(@0, @1)\";\n            }\n\n            return null;\n        }\n\n        public string ResolveMember(MemberInfo member) => null;\n        public string ResolveCtor(ConstructorInfo ctor) => null;\n    }\n}","sourceCodeStart":5,"sourceCodeEnd":33,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Mapper/Linq/TypeResolver/MathResolver.cs#L5-L33","documentation":"LiteDB's MathResolver only supports the two-argument overload of Math.Round (value + digits) which maps to ROUND(@0, @1). It throws ArgumentOutOfRangeException for the single-argument Math.Round(x) and the three-argument Math.Round(x, digits, MidpointRounding) because the BsonExpression ROUND function has no equivalents for those semantics.","triggerScenarios":"Using Math.Round(x) (banker's rounding to integer) or Math.Round(x, d, MidpointRounding.AwayFromZero) in a LINQ expression that LiteDB translates to BsonExpression.","commonSituations":"Rounding a salary or price field in a Where/Select. Using MidpointRounding.AwayFromZero expecting SQL-style rounding. Calling Math.Round(x) to round to the nearest integer.","solutions":["Use the two-argument overload: Math.Round(x, 0) rounds to integer with 0 fractional digits.","If you need MidpointRounding.AwayFromZero, round the value in memory before or after the query rather than in the LINQ expression.","Pre-compute rounded values and store them as a separate field."],"exampleFix":"// before\nvar q = col.Query().Select(x => Math.Round(x.Price)).ToList();\n// after\nvar q = col.Query().Select(x => Math.Round(x.Price, 0)).ToList();","handlingStrategy":"validation","validationCode":"// Always use the two-argument overload of Math.Round in LINQ expressions\n// Math.Round(x, digits) — NOT Math.Round(x) or Math.Round(x, d, MidpointRounding.X)\nvar q = col.Query().Select(x => Math.Round(x.Price, 2));","typeGuard":null,"tryCatchPattern":"try\n{\n    var q = col.Query().Select(x => Math.Round(x.Value, 2));\n}\ncatch (ArgumentOutOfRangeException ex) when (ex.Message.Contains(\"Round\"))\n{\n    // Use Math.Round(x, digits) with exactly 2 arguments\n    throw;\n}","preventionTips":["In LINQ expressions, always call Math.Round with exactly two arguments: Math.Round(x, digits).","For MidpointRounding.AwayFromZero semantics, round the value in memory after fetching it.","Store pre-rounded values in a dedicated field if rounding is needed frequently in queries."],"tags":["linq","type-resolver","math","round"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}