litedb-org/LiteDB · error · NotSupportedException

Constructor for {node.Type.Name} are not supported when conv

Error message

Constructor for {node.Type.Name} are not supported when convert to BsonExpression ({node.ToString()}).

What it means

Thrown by VisitNew when a new SomeType(args) expression is encountered, SomeType has a registered ITypeResolver, but ResolveCtor returns null for the constructor used. The type is recognized by the resolver system, but the specific constructor overload has no translation pattern.

Source

Thrown at LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs:360

            {
                base.VisitUnary(node);
            }

            return node;
        }

        /// <summary>
        /// Visit :: x => `new { x.Id, x.Name }`
        /// </summary>
        protected override Expression VisitNew(NewExpression node)
        {
            if (node.Members == null)
            {
                if (TryGetResolver(node.Type, out var type))
                {
                    var pattern = type.ResolveCtor(node.Constructor);

                    if (pattern == null) throw new NotSupportedException($"Constructor for {node.Type.Name} are not supported when convert to BsonExpression ({node.ToString()}).");

                    this.ResolvePattern(pattern, null, node.Arguments);
                }
                else
                {
                    throw new NotSupportedException($"New instance are not supported for {node.Type} when convert to BsonExpression ({node.ToString()}).");
                }
            }
            else
            {
                _builder.Append("{ ");

                for (var i = 0; i < node.Members.Count; i++)
                {
                    var member = node.Members[i];
                    _builder.Append(i > 0 ? ", " : "");
                    _builder.AppendFormat("'{0}': ", member.Name);
                    this.Visit(node.Arguments[i]);

View on GitHub (pinned to f906a5f850)

Solutions

  1. Use a supported constructor overload for the type (check the resolver source for recognized ctors).
  2. Hoist the value into a closure variable so the visitor evaluates it in memory instead of translating the constructor.
  3. Use a raw BsonExpression or a pre-computed constant.

Example fix

// before
col.Find(x => x.CreatedAt > new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc));
// unsupported DateTime ctor overload

// after -- hoist the constant
var cutoff = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc);
col.Find(x => x.CreatedAt > cutoff);
Defensive patterns

Strategy: validation

Validate before calling

// Hoist new SomeType(args) into a closure variable so it is evaluated in memory
var cutoff = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc); // evaluate outside
col.Find(x => x.CreatedAt > cutoff);

Try / catch

try
{
    var results = col.Find(x => x.CreatedAt > new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc));
}
catch (NotSupportedException ex) when (ex.Message.Contains("Constructor for"))
{
    var cutoff = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    results = col.Find(x => x.CreatedAt > cutoff);
}

Prevention

When it happens

Trigger: Using new with a resolver-backed type and a constructor the resolver does not support. For example new DateTime(2020, 1, 1, 12, 0, 0, DateTimeKind.Utc) if DateTimeResolver does not have a pattern for that 6-argument overload, or new Guid(byteArray) if GuidResolver lacks that ctor.

Common situations: Using an uncommon constructor overload of a built-in type in a LINQ query predicate or projection. Passing calendar/timezone arguments that the resolver does not map.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/dd2a81dfd54205ff. Report an issue: GitHub.