{"record":{"id":"7d8eb5dc11473056","repo":"litedb-org/LiteDB","slug":"fieldexpr","errorCode":null,"errorMessage":"fieldExpr","messagePattern":"fieldExpr","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Database/LiteQueryable.cs","lineNumber":249,"sourceCode":"        /// Project each document of resultset into a new document/value based on selector expression\n        /// </summary>\n        public ILiteQueryable<K> Select<K>(Expression<Func<T, K>> selector)\n        {\n            _query.Select = _mapper.GetExpression(selector);\n\n            return new LiteQueryable<K>(_engine, _mapper, _collection, _query);\n        }\n\n        private static void ValidateVectorArguments(float[] target, double maxDistance)\n        {\n            if (target == null || target.Length == 0) throw new ArgumentException(\"Target vector must be provided.\", nameof(target));\n            // Dot-product queries interpret \"maxDistance\" as a minimum similarity score and may therefore pass negative values.\n            if (double.IsNaN(maxDistance)) throw new ArgumentOutOfRangeException(nameof(maxDistance), \"Similarity threshold must be a valid number.\");\n        }\n\n        private static BsonExpression CreateVectorSimilarityFilter(BsonExpression fieldExpr, float[] target, double maxDistance)\n        {\n            if (fieldExpr == null) throw new ArgumentNullException(nameof(fieldExpr));\n\n            ValidateVectorArguments(target, maxDistance);\n\n            var targetArray = new BsonArray(target.Select(v => new BsonValue(v)));\n            return BsonExpression.Create($\"{fieldExpr.Source} VECTOR_SIM @0 <= @1\", targetArray, new BsonValue(maxDistance));\n        }\n\n        internal ILiteQueryable<T> VectorWhereNear(string vectorField, float[] target, double maxDistance)\n        {\n            if (string.IsNullOrWhiteSpace(vectorField)) throw new ArgumentNullException(nameof(vectorField));\n\n            var fieldExpr = BsonExpression.Create($\"$.{vectorField}\");\n            return this.VectorWhereNear(fieldExpr, target, maxDistance);\n        }\n\n        internal ILiteQueryable<T> VectorWhereNear(BsonExpression fieldExpr, float[] target, double maxDistance)\n        {\n            var filter = CreateVectorSimilarityFilter(fieldExpr, target, maxDistance);","sourceCodeStart":231,"sourceCodeEnd":267,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Database/LiteQueryable.cs#L231-L267","documentation":"Thrown by CreateVectorSimilarityFilter (the internal builder for VECTOR_SIM WHERE clauses) when fieldExpr is null. The field expression identifies which document field holds the stored vector; a null expression gives nothing to compare. Reached from VectorWhereNear(BsonExpression, ...).","triggerScenarios":"Calling VectorWhereNear((BsonExpression)null, target, maxDistance), or passing a BsonExpression variable that resolved to null (e.g. a failed BsonExpression.Create whose result was discarded).","commonSituations":"Dynamically building the field expression and a branch returns null, or destructuring a config-driven field name that is empty so the created expression is discarded.","solutions":["Prefer the string overload VectorWhereNear(string vectorField, ...) which constructs the expression for you and validates the name.","Null-check the BsonExpression before calling and throw a clearer domain error.","Construct the expression via BsonExpression.Create($.{field}) only after validating the field name."],"exampleFix":"// before\nvar q = col.Query().VectorWhereNear(fieldExpr, vec, dist);\n\n// after\nif (fieldExpr == null)\n    throw new InvalidOperationException(\"Vector field expression is required.\");\nvar q = col.Query().VectorWhereNear(fieldExpr, vec, dist);","handlingStrategy":"validation","validationCode":"if (fieldExpr == null)\n    throw new InvalidOperationException(\"Vector field expression is required.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer the string overload which constructs and validates the expression for you.","Null-check BsonExpression results from dynamic construction."],"tags":["vector","vector-search","argument-null","expression"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}