{"record":{"id":"d429bd1e51d358b5","repo":"litedb-org/LiteDB","slug":"there-is-no-tryparse-translate-use-guid-parse","errorCode":null,"errorMessage":"There is no TryParse translate. Use Guid.Parse()","messagePattern":"There is no TryParse translate\\. Use Guid\\.Parse\\(\\)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Mapper/Linq/TypeResolver/GuidResolver.cs","lineNumber":24,"sourceCode":"using System.Reflection;\nusing System.Text;\nusing static LiteDB.Constants;\n\nnamespace LiteDB\n{\n    internal class GuidResolver : ITypeResolver\n    {\n        public string ResolveMethod(MethodInfo method)\n        {\n            switch (method.Name)\n            {\n                // instance methods\n                case \"ToString\": return \"STRING(#)\";\n\n                // static methods\n                case \"NewGuid\": return \"GUID()\";\n                case \"Parse\": return \"GUID(@0)\";\n                case \"TryParse\": throw new NotSupportedException(\"There is no TryParse translate. Use Guid.Parse()\");\n                case \"Equals\": return \"# = @0\";\n            }\n\n            return null;\n        }\n\n        public string ResolveMember(MemberInfo member)\n        {\n            switch (member.Name)\n            {\n                // static properties\n                case \"Empty\": return \"GUID('00000000-0000-0000-0000-000000000000')\";\n            }\n\n            return null;\n        }\n\n        public string ResolveCtor(ConstructorInfo ctor)","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Mapper/Linq/TypeResolver/GuidResolver.cs#L6-L42","documentation":"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.","triggerScenarios":"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 _)).","commonSituations":"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.","solutions":["Replace Guid.TryParse with Guid.Parse in LINQ expressions sent to LiteDB.","Pre-validate the string outside the query and pass the parsed Guid as a parameter.","Store the Guid directly in the document instead of a string that needs parsing at query time."],"exampleFix":"// before\nvar results = col.Query().Where(x => Guid.TryParse(x.Token, out _)).ToList();\n// after\nvar g = Guid.Parse(tokenStr);\nvar results = col.Query().Where(x => x.TokenId == g).ToList();","handlingStrategy":"validation","validationCode":"// Validate/parse the Guid outside the LINQ expression before querying\nif (!Guid.TryParse(inputString, out var parsedId))\n    throw new FormatException($\"Invalid GUID: {inputString}\");\nvar results = col.Query().Where(x => x.Id == parsedId).ToList();","typeGuard":null,"tryCatchPattern":"try\n{\n    var q = col.Query().Where(x => x.Id == Guid.Parse(tokenString));\n}\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"TryParse\"))\n{\n    // Parse the Guid before the query instead of inside it\n    throw;\n}","preventionTips":["Never use Guid.TryParse in LINQ expressions sent to LiteDB; always use Guid.Parse.","Parse Guids in application code and pass the result as a query parameter.","Store Guids natively rather than as strings requiring parse at query time."],"tags":["linq","type-resolver","guid","tryparse"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}