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

Logical Negation Operator and Comparing False Have Different Results

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.9.2
    • Component/s: Linq
    • Labels:
      None

      Comparing a property to false using the Equality Comparer gives different results to using the Logical Negation Operator on the property.

      See the following code snippet...

       

      using Mongo2Go;
      using MongoDB.Bson;
      using MongoDB.Bson.Serialization.Conventions;
      using MongoDB.Driver;
      using MongoDB.Driver.Linq;namespace ConsoleApp11
      {
          class Program
          {
              static void Main(string[] args)
              {
                  using (MongoDbRunner mongoRunner = MongoDbRunner.Start())
                  {
                      // Ignore the default values. So a false boolean will not be stored in Mongo
                      ConventionRegistry.Register("Ignore default values", new ConventionPack { new IgnoreIfDefaultConvention(true) }, t => true);                
       var mongoClient = new MongoClient(mongoRunner.ConnectionString);                
      var mongoDb = mongoClient.GetDatabase("TestDb");               
       var collection = mongoDb.GetCollection<SimpleDocument>("TestCollection");               
       var documentTwo = new SimpleDocument
                      {
                          Property = false
                      };                collection.InsertOne(documentTwo);                
      var query1 = collection.AsQueryable().Where(x => x.Property == false).ToList(); // Doesn't find the record         
             var query2 = collection.AsQueryable().Where(x => !x.Property).ToList(); // Finds the record
                  }
              }
          }    public class SimpleDocument
          {
              public ObjectId Id { get; set; }
              public bool Property { get; set; }
          }
      }
      

      I would have expected both query1 and query2 to find the record.

       

            Assignee:
            dmitry.lukyanov@mongodb.com Dmitry Lukyanov (Inactive)
            Reporter:
            chrismcewan@ncfe.org.uk Christopher McEwen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: