-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: 1.4.2, 1.5
-
Component/s: None
-
None
Consider the following code:
int? nullableFive = 5;
var greaterThanFive = collection.AsQueryable().Where(d => d.IntField > nullableFive) // where IntField is of type int (non-nullable)
In standard LINQ implementations a comparison between a nullable and non-nullable field is valid if the nullable field has a value. If the nullable field does not have a value, the comparison will return false.
Workaround:
var greaterThanFive = collection.AsQueryable().Where(d => d.IntField > nullableFive.Value)
System..InvalidOperationException: The operands for operator 'Equal' do not match the parameters of method 'op_Equality'. at System.Linq.Expressions.Expression.GetMethodBasedBinaryOperator(ExpressionType binaryType, Expression left, Expression right, MethodInfo method, Boolean liftToNull) at System.Linq.Expressions.Expression.Equal(Expression left, Expression right, Boolean liftToNull, MethodInfo method) at System.Linq.Expressions.Expression.MakeBinary(ExpressionType binaryType, Expression left, Expression right, Boolean liftToNull, MethodInfo method, LambdaExpression conversion) at MongoDB.Driver.Linq.ExpressionVisitor.VisitBinary(BinaryExpression node) at MongoDB.Driver.Linq.ExpressionNormalizer.VisitBinary(BinaryExpression node) at MongoDB.Driver.Linq.ExpressionVisitor.Visit(Expression node) at MongoDB.Driver.Linq.ExpressionVisitor.VisitLambda(LambdaExpression node) at MongoDB.Driver.Linq.ExpressionVisitor.Visit(Expression node) at MongoDB.Driver.Linq.ExpressionVisitor.VisitUnary(UnaryExpression node) at MongoDB.Driver.Linq.ExpressionVisitor.Visit(Expression node) at MongoDB.Driver.Linq.ExpressionVisitor.Visit(ReadOnlyCollection1 nodes) at MongoDB.Driver.Linq.ExpressionVisitor.VisitMethodCall(MethodCallExpression node) at MongoDB.Driver.Linq.ExpressionVisitor.Visit(Expression node) at MongoDB.Driver.Linq.MongoQueryTranslator.Translate(MongoQueryProvider provider, Expression expression) at MongoDB.Driver.Linq.MongoQueryProvider.Execute[TResult](Expression expression)
- is duplicated by
-
CSHARP-474 Review all exceptions thrown by the C# driver
- Closed
- related to
-
CSHARP-1867 Non-nullable members cannot be compared to nullable values
- Closed