litedb-org/LiteDB · error · NotSupportedException
Cast from {fromType.Name} are not supported when convert to
Error message
Cast from {fromType.Name} are not supported when convert to BsonExpression What it means
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.
Source
Thrown at LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs:324
}
}
else if (node.NodeType == ExpressionType.Convert)
{
var fromType = node.Operand.Type;
var toType = node.Type;
// do Numeric cast only from "Double/Decimal" to "Int32/Int64"
if ((fromType == typeof(Double) || fromType == typeof(Decimal)) &&
(toType == typeof(Int32) || toType == typeof(Int64)))
{
var methodName = "To" + toType.Name.ToString();
var convert = typeof(Convert).GetMethods()
.Where(x => x.Name == methodName)
.Where(x => x.GetParameters().Length == 1 && x.GetParameters().Any(z => z.ParameterType == fromType))
.FirstOrDefault();
if (convert == null) throw new NotSupportedException($"Cast from {fromType.Name} are not supported when convert to BsonExpression");
var method = Expression.Call(null, convert, node.Operand);
this.VisitMethodCall(method);
}
else
{
base.VisitUnary(node);
}
}
else if (node.NodeType == ExpressionType.ArrayLength)
{
_builder.Append("LENGTH(");
this.Visit(node.Operand);
_builder.Append(")");
}
else
{View on GitHub (pinned to f906a5f850)
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.
Defensive patterns
Strategy: validation
Validate before calling
// Avoid explicit Double/Decimal-to-int casts in LINQ queries; precompute instead var intScore = (int)entity.Score; // compute in memory col.Find(x => x.Value > intScore);
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- 0
- Extend expression must return an anonymous class to be merge
- Multiple OrderBy calls are not supported. Use ThenBy for add
- GROUP BY already defined in this query
- field
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/54bac91963a6b230.
Report an issue: GitHub.