Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-506

Throw better exception when comparing a non-nullable field to a nullable value

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.4
    • 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)

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            zaidmasud Zaid Masud
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: