{"record":{"id":"54bac91963a6b230","repo":"litedb-org/LiteDB","slug":"cast-from-fromtype-name-are-not-supported-when-c","errorCode":null,"errorMessage":"Cast from {fromType.Name} are not supported when convert to BsonExpression","messagePattern":"Cast from (.+?) are not supported when convert to BsonExpression","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs","lineNumber":324,"sourceCode":"                }\n            }\n            else if (node.NodeType == ExpressionType.Convert)\n            {\n                var fromType = node.Operand.Type;\n                var toType = node.Type;\n\n                // do Numeric cast only from \"Double/Decimal\" to \"Int32/Int64\"\n                if ((fromType == typeof(Double) || fromType == typeof(Decimal)) &&\n                    (toType == typeof(Int32) || toType == typeof(Int64)))\n                {\n                    var methodName = \"To\" + toType.Name.ToString();\n\n                    var convert = typeof(Convert).GetMethods()\n                        .Where(x => x.Name == methodName)\n                        .Where(x => x.GetParameters().Length == 1 && x.GetParameters().Any(z => z.ParameterType == fromType))\n                        .FirstOrDefault();\n\n                    if (convert == null) throw new NotSupportedException($\"Cast from {fromType.Name} are not supported when convert to BsonExpression\");\n\n                    var method = Expression.Call(null, convert, node.Operand);\n\n                    this.VisitMethodCall(method);\n                }\n                else\n                {\n                    base.VisitUnary(node);\n                }\n            }\n            else if (node.NodeType == ExpressionType.ArrayLength)\n            {\n                _builder.Append(\"LENGTH(\");\n                this.Visit(node.Operand);\n                _builder.Append(\")\");\n            }\n            else\n            {","sourceCodeStart":306,"sourceCodeEnd":342,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs#L306-L342","documentation":"Thrown by VisitUnary (Convert node) when casting from Double or Decimal to Int32 or Int64 but reflection cannot find the matching Convert.ToXxx method. The visitor only handles this specific Double/Decimal to Int32/Int64 path and uses typeof(Convert).GetMethods to locate the exact overload.","triggerScenarios":"A LINQ expression containing an explicit numeric cast from Double/Decimal to Int32/Int64 (e.g., x => (int)x.Score) where the reflection lookup for Convert.ToInt32(Double) or Convert.ToInt64(Decimal) fails to return a method.","commonSituations":"Practically unreachable on standard .NET runtimes because Convert.ToInt32(Double), Convert.ToInt32(Decimal), Convert.ToInt64(Double), and Convert.ToInt64(Decimal) all exist in the BCL. Could theoretically surface on a trimmed/stripped runtime that removes these overloads, or on an unusual runtime implementation.","solutions":["This should not occur on standard .NET; if it does, report it as a LiteDB bug with the runtime details.","Avoid explicit Double/Decimal-to-int casts inside LINQ queries; cast the value in memory before building the expression."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Avoid explicit Double/Decimal-to-int casts in LINQ queries; precompute instead\nvar intScore = (int)entity.Score; // compute in memory\ncol.Find(x => x.Value > intScore);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid explicit numeric casts inside LINQ query expressions; cast values in memory before building the query.","This error is practically unreachable on standard .NET -- if it occurs, report it as a bug.","Use simpler expressions that do not require implicit Convert nodes."],"tags":["linq","cast","numeric"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}