{"record":{"id":"89c2468daf187d2c","repo":"JamesNK/Newtonsoft.Json","slug":"object-must-be-of-type-guid","errorCode":null,"errorMessage":"Object must be of type Guid.","messagePattern":"Object must be of type Guid\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JValue.cs","lineNumber":374,"sourceCode":"                        }\n\n                        return offsetA.CompareTo(offsetB);\n                    }\n#endif\n                case JTokenType.Bytes:\n                    if (!(objB is byte[] bytesB))\n                    {\n                        throw new ArgumentException(\"Object must be of type byte[].\");\n                    }\n\n                    byte[]? bytesA = objA as byte[];\n                    MiscellaneousUtils.Assert(bytesA != null);\n\n                    return MiscellaneousUtils.ByteArrayCompare(bytesA!, bytesB);\n                case JTokenType.Guid:\n                    if (!(objB is Guid))\n                    {\n                        throw new ArgumentException(\"Object must be of type Guid.\");\n                    }\n\n                    Guid guid1 = (Guid)objA;\n                    Guid guid2 = (Guid)objB;\n\n                    return guid1.CompareTo(guid2);\n                case JTokenType.Uri:\n                    Uri? uri2 = objB as Uri;\n                    if (uri2 == null)\n                    {\n                        throw new ArgumentException(\"Object must be of type Uri.\");\n                    }\n\n                    Uri uri1 = (Uri)objA;\n\n                    return Comparer<string>.Default.Compare(uri1.ToString(), uri2.ToString());\n                case JTokenType.TimeSpan:\n                    if (!(objB is TimeSpan))","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JValue.cs#L356-L392","documentation":"Thrown by JValue.CompareTo when the token's JSON type is JTokenType.Guid and the operand is not a Guid. Two Guid JValues are compared with Guid.CompareTo; a non-Guid operand has no ordering relationship, so CompareTo throws ArgumentException. This mirrors the type-strict comparison behaviour of all primitive JValue token types.","triggerScenarios":"Comparing a Guid JValue against a string like token.CompareTo(\"00000000-...\") instead of a Guid instance, or against an int/other primitive. Triggered during sorting or equality checks inside JObject/JArray operations.","commonSituations":"Treating a GUID field as a string in comparison logic; deserializing database uniqueidentifier columns then comparing against string literals in LINQ-to-JSON queries.","solutions":["Convert the operand to Guid first: token.CompareTo(Guid.Parse(text)).","Use token.Value<Guid>() and compare the resulting Guid values with the standard Guid comparer.","Type-check the operand before calling CompareTo."],"exampleFix":"// before\nint c = guidToken.CompareTo(\"d3b0c1f2-...\");\n\n// after\nint c = guidToken.CompareTo(Guid.Parse(\"d3b0c1f2-...\"));","handlingStrategy":"type-guard","validationCode":"if (other is Guid g) { int c = guidToken.CompareTo(g); }","typeGuard":"static bool IsGuid(object? o) => o is Guid;","tryCatchPattern":"try { int c = token.CompareTo(other); } catch (ArgumentException ex) when (ex.Message.Contains(\"Guid\")) { /* parse string to Guid */ }","preventionTips":["Parse string operands to Guid before comparing Guid JValues.","Standardize on Guid for GUID-typed fields.","Type-check operands in heterogeneous comparisons."],"tags":["jvalue","compareto","guid","invalid-cast","argumentexception"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}