litedb-org/LiteDB · error · NotSupportedException

There is no TryParse translate. Use Guid.Parse()

Error message

There is no TryParse translate. Use Guid.Parse()

What it means

LiteDB's LINQ-to-BsonExpression translator has a type resolver for Guid that maps Guid.Parse to GUID(@0) but explicitly rejects Guid.TryParse. The TryParse pattern returns a bool plus an out parameter, which has no equivalent in BsonExpression syntax, so the resolver throws NotSupportedException to give a clear directive.

Source

Thrown at LiteDB/Client/Mapper/Linq/TypeResolver/GuidResolver.cs:24

using System.Reflection;
using System.Text;
using static LiteDB.Constants;

namespace LiteDB
{
    internal class GuidResolver : ITypeResolver
    {
        public string ResolveMethod(MethodInfo method)
        {
            switch (method.Name)
            {
                // instance methods
                case "ToString": return "STRING(#)";

                // static methods
                case "NewGuid": return "GUID()";
                case "Parse": return "GUID(@0)";
                case "TryParse": throw new NotSupportedException("There is no TryParse translate. Use Guid.Parse()");
                case "Equals": return "# = @0";
            }

            return null;
        }

        public string ResolveMember(MemberInfo member)
        {
            switch (member.Name)
            {
                // static properties
                case "Empty": return "GUID('00000000-0000-0000-0000-000000000000')";
            }

            return null;
        }

        public string ResolveCtor(ConstructorInfo ctor)

View on GitHub (pinned to f906a5f850)

Solutions

  1. Replace Guid.TryParse with Guid.Parse in LINQ expressions sent to LiteDB.
  2. Pre-validate the string outside the query and pass the parsed Guid as a parameter.
  3. Store the Guid directly in the document instead of a string that needs parsing at query time.

Example fix

// before
var results = col.Query().Where(x => Guid.TryParse(x.Token, out _)).ToList();
// after
var g = Guid.Parse(tokenStr);
var results = col.Query().Where(x => x.TokenId == g).ToList();
Defensive patterns

Strategy: validation

Validate before calling

// Validate/parse the Guid outside the LINQ expression before querying
if (!Guid.TryParse(inputString, out var parsedId))
    throw new FormatException($"Invalid GUID: {inputString}");
var results = col.Query().Where(x => x.Id == parsedId).ToList();

Try / catch

try
{
    var q = col.Query().Where(x => x.Id == Guid.Parse(tokenString));
}
catch (NotSupportedException ex) when (ex.Message.Contains("TryParse"))
{
    // Parse the Guid before the query instead of inside it
    throw;
}

Prevention

When it happens

Trigger: Using Guid.TryParse(s, out var g) inside a LINQ predicate or projection that LiteDB must translate to a BsonExpression, e.g. collection.Query().Where(x => Guid.TryParse(x.Name, out _)).

Common situations: Copy-pasting validation logic from the business layer into a database query. Using TryParse for safe parsing without realizing the query must execute server-side in LiteDB's expression engine.

Related errors


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