{"record":{"id":"70950bad3ed428f9","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-gettype-to-biginteger","errorCode":null,"errorMessage":"Cannot convert {value.GetType()} to BigInteger.","messagePattern":"Cannot convert (.+?) to BigInteger\\.","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":810,"sourceCode":"            if (value is long l)\n            {\n                return new BigInteger(l);\n            }\n            if (value is uint u)\n            {\n                return new BigInteger(u);\n            }\n            if (value is ulong @ulong)\n            {\n                return new BigInteger(@ulong);\n            }\n\n            if (value is byte[] bytes)\n            {\n                return new BigInteger(bytes);\n            }\n\n            throw new InvalidCastException($\"Cannot convert {value.GetType()} to BigInteger.\");\n        }\n    }\n}\n","sourceCodeStart":792,"sourceCodeEnd":814,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L792-L814","documentation":"This InvalidCastException is thrown by the private ToBigInteger helper (called from CompareBigInteger via Compare) when a numeric JSON value cannot be represented as a BigInteger — i.e. it is a double/decimal that is not an integer, or a type outside the handled set.","triggerScenarios":"Comparing JsonDynamicValue numeric values where one side is a non-integral number (e.g. 3.14) and the comparison path needs BigInteger conversion; comparing very large doubles with integers.","commonSituations":"Sorting JSON numbers that mix integers and decimals (e.g. [1, 2.5, 3]); comparing price fields (decimals) against integer quantities.","solutions":["Ensure compared numeric values are integral (integers) before comparing","Convert decimals/doubles to a common type and compare as double yourself","Round/truncate to integer before comparing if the domain allows","Compare strings ordinally as a fallback for uniform handling"],"exampleFix":"// before\nitems.OrderBy(x => x.Value) // 1 vs 2.5 hits BigInteger conversion\n// after\nitems.OrderBy(x => Convert.ToDouble(x.Value?.GetObjectValue()))","handlingStrategy":"type-guard","validationCode":"static bool IsIntegralNumber(object? v) => v is JsonElement el &&\n    el.ValueKind == JsonValueKind.Number &&\n    (el.TryGetInt64(out _) || el.TryGetUInt64(out _));","typeGuard":"static bool IsBigIntegerSafe(JsonDynamicValue? v) =>\n    v?.GetObjectValue() is JsonElement el && el.ValueKind == JsonValueKind.Number &&\n    el.TryGetInt64(out _);","tryCatchPattern":"int cmp;\ntry { cmp = a.CompareTo(b); }\ncatch (InvalidCastException ex) { /* log, compare as double */\n    cmp = Convert.ToDouble(a?.GetObjectValue()).CompareTo(Convert.ToDouble(b?.GetObjectValue())); }","preventionTips":["Avoid comparing mixed integer/decimal JSON values via JsonDynamicValue comparison","Compare numeric fields as double/decimal yourself when decimals are possible","Keep numeric fields consistently typed across documents","Test sorting with real data containing decimals"],"tags":["invalid-cast","json","bigint","numeric-conversion"],"backgroundTag":"incompatible-source-type","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}