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

String IsNullOrEmpty does not test for fields ignored if null

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.0.1, 2.1
    • Affects Version/s: 1.7.1
    • Component/s: Linq
    • Labels:
      None

      String.IsNullOrEmpty(x.S) expands to
      "$or" : [{ "S" :

      { "$type" : 10 }

      },

      { "S" : "" }

      ]

      If a field has BsonIgnoreIfNull, it should also test

      { "S" : null }

      Here's a test illustrating this:

              public class WithString
              {
                  [BsonIgnoreIfNull]
                  public string S { get; set; }
              }
      
              [Test]
              public void StringIsNullOrEmptyShouldTestIgnoredNulls()
              {
              
                  var a = new WithString() {S = "ooo"};
                  var b = new WithString() {S = ""};
                  var c = new WithString() { };
      
                  collection.Insert(a);
                  collection.Insert(b);
                  collection.Insert(c);
      
                  var query = from t in collection.AsQueryable()
                              where string.IsNullOrEmpty(t.S)
                              select t;
      
                  var translated = MongoQueryTranslator.Translate(query);
                  var mongoQuery = ((SelectQuery)translated).BuildQuery();
                  Debug.WriteLine(mongoQuery.ToString());
      
                  var results = query.ToList();
                  Assert.That(results.Count, Is.EqualTo(2));
              }
      

            Assignee:
            craig.wilson@mongodb.com Craig Wilson
            Reporter:
            andy clapham Andy Clapham
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: